From b76961c91afe5d21c2211b709251dca198f3401b Mon Sep 17 00:00:00 2001 From: cbuie Date: Sun, 21 Aug 2016 08:35:38 -0500 Subject: [PATCH 01/13] test trying to commit this file to dir --- code/cb_dictionary.py | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 code/cb_dictionary.py diff --git a/code/cb_dictionary.py b/code/cb_dictionary.py new file mode 100644 index 0000000..4e4366a --- /dev/null +++ b/code/cb_dictionary.py @@ -0,0 +1,6 @@ +# this file should sync across my system + +def thisWorked (): + print ('Your file is found.') + +thisWorked() \ No newline at end of file From 19df62c3f287808315d8d4a1f50f6b3fecf019b6 Mon Sep 17 00:00:00 2001 From: cbuie Date: Sun, 21 Aug 2016 08:41:28 -0500 Subject: [PATCH 02/13] check --- code/cb_dictionary.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/code/cb_dictionary.py b/code/cb_dictionary.py index 4e4366a..3e5d2cb 100644 --- a/code/cb_dictionary.py +++ b/code/cb_dictionary.py @@ -3,4 +3,9 @@ def thisWorked (): print ('Your file is found.') + + + +#success this file has been edited on multiple systems + thisWorked() \ No newline at end of file From 37b19a414180782e6e2678e1c7955bca18349f22 Mon Sep 17 00:00:00 2001 From: Chris L Buie Date: Sun, 21 Aug 2016 09:09:53 -0500 Subject: [PATCH 03/13] my update --- code/cb_dictionary.py | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/code/cb_dictionary.py b/code/cb_dictionary.py index 3e5d2cb..eb6107b 100644 --- a/code/cb_dictionary.py +++ b/code/cb_dictionary.py @@ -1,11 +1,25 @@ -# this file should sync across my system +# General notes working with dictionaries from ThinkPython -def thisWorked (): - print ('Your file is found.') +#create dictionary: +x = dict() +x = {} +print x +# create empty dictionary of eng to spanish words: -#success this file has been edited on multiple systems +eng2sp = dict() -thisWorked() \ No newline at end of file +#add elements to the dict() + +# eng2sp['one'] = 'uno' +# print eng2sp + +#or + +x = ('one', 'two', 'three') +y = ('uno', 'dos', 'tres') + +for i in range(len(x)): + eng2sp[x[i]] = y[i] From 642dd106a4a7f54d639efbe39d525410991baff7 Mon Sep 17 00:00:00 2001 From: cbuie Date: Sun, 21 Aug 2016 09:10:44 -0500 Subject: [PATCH 04/13] test trying to commit this file to dir --- code/cb_dictionary.py | 1 + 1 file changed, 1 insertion(+) diff --git a/code/cb_dictionary.py b/code/cb_dictionary.py index eb6107b..81e7d3e 100644 --- a/code/cb_dictionary.py +++ b/code/cb_dictionary.py @@ -23,3 +23,4 @@ for i in range(len(x)): eng2sp[x[i]] = y[i] +print eng2sp \ No newline at end of file From 0b282296a4b5ba6b520a5b7da37be51cbb77da3a Mon Sep 17 00:00:00 2001 From: cbuie Date: Sun, 21 Aug 2016 09:11:40 -0500 Subject: [PATCH 05/13] test trying to commit this file to dir --- code/cb_dictionary.py | 1 + 1 file changed, 1 insertion(+) diff --git a/code/cb_dictionary.py b/code/cb_dictionary.py index 81e7d3e..76cb147 100644 --- a/code/cb_dictionary.py +++ b/code/cb_dictionary.py @@ -23,4 +23,5 @@ for i in range(len(x)): eng2sp[x[i]] = y[i] + print eng2sp \ No newline at end of file From 2da93763f8257441632b35266087587a30c66aea Mon Sep 17 00:00:00 2001 From: Chris L Buie Date: Sun, 21 Aug 2016 10:58:12 -0500 Subject: [PATCH 06/13] my update --- code/cb_dictionary.py | 62 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 58 insertions(+), 4 deletions(-) diff --git a/code/cb_dictionary.py b/code/cb_dictionary.py index 76cb147..f68c3de 100644 --- a/code/cb_dictionary.py +++ b/code/cb_dictionary.py @@ -1,6 +1,6 @@ # General notes working with dictionaries from ThinkPython -#create dictionary: +# create dictionary: x = dict() x = {} @@ -11,12 +11,12 @@ eng2sp = dict() -#add elements to the dict() +# add elements to the dict() # eng2sp['one'] = 'uno' # print eng2sp -#or +# or x = ('one', 'two', 'three') y = ('uno', 'dos', 'tres') @@ -24,4 +24,58 @@ for i in range(len(x)): eng2sp[x[i]] = y[i] -print eng2sp \ No newline at end of file +print eng2sp + +''' +#interesting! +The in operator uses different algorithms for lists and dictionaries. For lists, it uses a search algorithm, as in Section 8.6. As the list gets longer, the search time gets longer in direct proportion. +For dictionaries, Python uses an algorithm called a hashtable that has a remarkable property: the in operator takes about the same amount of time no matter how many items there are in a dictionary. +''' + +with open('code/words.txt', mode='r') as fd: + words = fd.read().splitlines() + +result = {} + + +def wordDict(): + for line in words: + result[line] = words.index(line) + return result + + +print wordDict() + + + +import uuid + +with open('code/words.txt') as fd: + words = fd.read().splitlines() + +result = dict() + + +def dictionary(): + for line in words: + result[line] = uuid.uuid4() + return result + +print dictionary() + + +#Dictionary as counters: +#very nice! + +''' +The first line of the function creates an empty dictionary. The for loop traverses the string. Each time through the loop, if the character c is not in the dictionary, we create a new item with key c and the initial value 1 (since we have seen this letter once). If c is already in the dictionary we increment d[c] +''' + +def histogram(s): + d = dict() + for c in s: + if c not in d: + d[c] = 1 + else: + d[c] += 1 + return d \ No newline at end of file From 8c98f2b1a5f8be386965ce62ff83d7248b643ff1 Mon Sep 17 00:00:00 2001 From: Chris L Buie Date: Sun, 21 Aug 2016 10:58:21 -0500 Subject: [PATCH 07/13] my update --- code/cb_dictionary.py | 1 + 1 file changed, 1 insertion(+) diff --git a/code/cb_dictionary.py b/code/cb_dictionary.py index f68c3de..b52a788 100644 --- a/code/cb_dictionary.py +++ b/code/cb_dictionary.py @@ -71,6 +71,7 @@ def dictionary(): The first line of the function creates an empty dictionary. The for loop traverses the string. Each time through the loop, if the character c is not in the dictionary, we create a new item with key c and the initial value 1 (since we have seen this letter once). If c is already in the dictionary we increment d[c] ''' + def histogram(s): d = dict() for c in s: From 0cd4bfe37f197f1a80c37e3ca7407275dd7279a9 Mon Sep 17 00:00:00 2001 From: Chris L Buie Date: Sun, 21 Aug 2016 12:52:13 -0500 Subject: [PATCH 08/13] my update --- code/cb_dictionary.py | 115 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 113 insertions(+), 2 deletions(-) diff --git a/code/cb_dictionary.py b/code/cb_dictionary.py index b52a788..27a3759 100644 --- a/code/cb_dictionary.py +++ b/code/cb_dictionary.py @@ -68,7 +68,9 @@ def dictionary(): #very nice! ''' -The first line of the function creates an empty dictionary. The for loop traverses the string. Each time through the loop, if the character c is not in the dictionary, we create a new item with key c and the initial value 1 (since we have seen this letter once). If c is already in the dictionary we increment d[c] +The first line of the function creates an empty dictionary. The for loop traverses the string. Each time through the loop, +if the character c is not in the dictionary, we create a new item with key c and the initial value 1 (since we have seen this letter once). +If c is already in the dictionary we increment d[c] ''' @@ -79,4 +81,113 @@ def histogram(s): d[c] = 1 else: d[c] += 1 - return d \ No newline at end of file + return d + +''' +Dictionaries have a method called get that takes a key and a default value. If the key appears in the dictionary, get returns the corresponding +value; otherwise it returns the default value. For example: +>>> h = histogram('a') +>>> print h +{'a': 1} +>>> h.get('a', 0) +1 +>>> h.get('b', 0) +0 +''' + +def histogram(word): + dictionary = dict() + for character in word: + dictionary[character] = 1 + dictionary.get(character, 0) + return dictionary + +print histogram('antidisestablishmentarianism').sort() + +''' +Given a dictionary d and a key k, it is easy to find the corresponding value v = d[k]. +This operation is called a lookup. +''' + +#reverse lookup: + +def reverse_lookup(d, v): + for k in d: + if d[k] == v: + return k + # raise can take a detailed error message + raise ValueError ('value not found in dictionary') + + +h = histogram('parrot'); print h +k = reverse_lookup(h,3); print k + +for i in h.values(): + results = [] + for k in h: + if h[k] == i: + results.append(list(i,h[k])) + else: + continue +print results + + +def invert_dict(d): + inv = dict() + for key in d: + val = d[key] + if val not in inv: + inv[val] = [key] + else: + inv[val].append(key) + return inv + + +##Exercise 9 +''' +If you did Exercise 8, you already have a function named has_duplicates that takes a list as a parameter and +returns True if there is any object that appears more than once in the list. +''' + +l = ['red', 'yellow', 'green'] + +def has_duplicates(t): + """Checks whether any element appears more than once in a sequence. + + Simple version using a for loop. + + t: sequence + """ + d = {} + for x in t: + if x in d: + return True + d[x] = True + return False + + +def has_duplicates2(t): + """Checks whether any element appears more than once in a sequence. + + Faster version using a set. + + t: sequence + """ + return len(set(t)) < len(t) + + +if __name__ == '__main__': + t = [1, 2, 3] + print has_duplicates(t) + t.append(1) + print has_duplicates(t) + + t = [1, 2, 3] + print has_duplicates2(t) + t.append(1) + print has_duplicates2(t) + + + +def has_duplicates3(l): + return len(set(l)) < len(l) + From 32d890a497dc318201ba667b67c395f3a05a1ef8 Mon Sep 17 00:00:00 2001 From: Chris L Buie Date: Sun, 21 Aug 2016 12:55:04 -0500 Subject: [PATCH 09/13] my update --- code/cb_tuples.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 code/cb_tuples.py diff --git a/code/cb_tuples.py b/code/cb_tuples.py new file mode 100644 index 0000000..e69de29 From 90b16edcf77bc387ca481d165a18f473e6a28f38 Mon Sep 17 00:00:00 2001 From: Chris L Buie Date: Sun, 21 Aug 2016 13:02:26 -0500 Subject: [PATCH 10/13] my update --- code/cb_tuples.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/code/cb_tuples.py b/code/cb_tuples.py index e69de29..6b7d828 100644 --- a/code/cb_tuples.py +++ b/code/cb_tuples.py @@ -0,0 +1,13 @@ +''' +Tuples are immutable + +A tuple is a sequence of values. The values can be any type, and they are indexed by integers, so in that respect tuples are a lot like lists. +The important difference is that tuples are immutable. +''' +s = 'a' +t = 'a', #a tuple of one needs comma else it is a str +print type(s), type(t) + +t = 'a','b','c' #or +t = ('a','b','c') #not necessary +print t, type(t) \ No newline at end of file From 1291510e6930d66685ceae284508a0dceb9ce9e5 Mon Sep 17 00:00:00 2001 From: Chris L Buie Date: Sun, 21 Aug 2016 13:46:08 -0500 Subject: [PATCH 11/13] my update --- code/cb_tuples.py | 61 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-) diff --git a/code/cb_tuples.py b/code/cb_tuples.py index 6b7d828..a562fea 100644 --- a/code/cb_tuples.py +++ b/code/cb_tuples.py @@ -10,4 +10,63 @@ t = 'a','b','c' #or t = ('a','b','c') #not necessary -print t, type(t) \ No newline at end of file +print t, type(t) + + +# creating multiple tuples at the same time is as easy as: +a, b , c = 'one' , 'two', 3 +print a, b, c + + +def sumIt(x): + result = 0 + for i in x: + result =+ i + return result + +def sum_all(*args): + return sum(args) + + +def sum_all(*args): + return sum(args) +print sum_all(1, 2, 3) +print sum_all(1, 2, 3, 4, 5) +print sum_all(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) + + +''' +zip is a built-in function that takes two or more sequences and zips them into a list of tuples where each tuple contains one element from each sequence. +In Python 3, zip returns an iterator of tuples, but for most purposes, an iterator behaves like a list. + +''' + +s = 'abc' +t = [0,1,2] +z = zip(s,t); print z + +#If the sequences are not the same length, the result has the length of the shorter one. + +print(zip('anne','elk')) + +#You can use tuple assignment in a for loop to traverse a list of tuples: + +t = [('a', 0), ('b', 1), ('c', 2)] +for letter, number in t: + print number, letter + +''' +If you combine zip, for and tuple assignment, you get a useful idiom for traversing two (or more) sequences at the same time. For example, has_match takes two sequences, +t1 and t2, and returns True if there is an index i such that t1[i] == t2[i]: +''' +def has_match(t1, t2): + for x, y in zip(t1, t2): + if x == y: + return True + return False + + +#If you need to traverse the elements of a sequence and their indices, you can use the built-in function enumerate: + +for index, element in enumerate('christopher Buie'): + print index, element From 023ebb1f61f4b428a2c6203204bea2dcefd1f50d Mon Sep 17 00:00:00 2001 From: Chris L Buie Date: Sun, 21 Aug 2016 14:30:41 -0500 Subject: [PATCH 12/13] my update --- code/cb_tuples.py | 55 ++++++++++++++++++++++++++++++++++++----------- 1 file changed, 42 insertions(+), 13 deletions(-) diff --git a/code/cb_tuples.py b/code/cb_tuples.py index a562fea..d966f65 100644 --- a/code/cb_tuples.py +++ b/code/cb_tuples.py @@ -5,36 +5,37 @@ The important difference is that tuples are immutable. ''' s = 'a' -t = 'a', #a tuple of one needs comma else it is a str +t = 'a', # a tuple of one needs comma else it is a str print type(s), type(t) -t = 'a','b','c' #or -t = ('a','b','c') #not necessary +t = 'a', 'b', 'c' # or +t = ('a', 'b', 'c') # not necessary print t, type(t) - # creating multiple tuples at the same time is as easy as: -a, b , c = 'one' , 'two', 3 +a, b, c = 'one', 'two', 3 print a, b, c def sumIt(x): result = 0 for i in x: - result =+ i + result = + i return result + def sum_all(*args): return sum(args) def sum_all(*args): return sum(args) + + print sum_all(1, 2, 3) print sum_all(1, 2, 3, 4, 5) print sum_all(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) - ''' zip is a built-in function that takes two or more sequences and zips them into a list of tuples where each tuple contains one element from each sequence. In Python 3, zip returns an iterator of tuples, but for most purposes, an iterator behaves like a list. @@ -42,14 +43,15 @@ def sum_all(*args): ''' s = 'abc' -t = [0,1,2] -z = zip(s,t); print z +t = [0, 1, 2] +z = zip(s, t); +print z -#If the sequences are not the same length, the result has the length of the shorter one. +# If the sequences are not the same length, the result has the length of the shorter one. -print(zip('anne','elk')) +print(zip('anne', 'elk')) -#You can use tuple assignment in a for loop to traverse a list of tuples: +# You can use tuple assignment in a for loop to traverse a list of tuples: t = [('a', 0), ('b', 1), ('c', 2)] for letter, number in t: @@ -59,6 +61,8 @@ def sum_all(*args): If you combine zip, for and tuple assignment, you get a useful idiom for traversing two (or more) sequences at the same time. For example, has_match takes two sequences, t1 and t2, and returns True if there is an index i such that t1[i] == t2[i]: ''' + + def has_match(t1, t2): for x, y in zip(t1, t2): if x == y: @@ -66,7 +70,32 @@ def has_match(t1, t2): return False -#If you need to traverse the elements of a sequence and their indices, you can use the built-in function enumerate: +# If you need to traverse the elements of a sequence and their indices, you can use the built-in function enumerate: for index, element in enumerate('christopher Buie'): print index, element + + +# For example, suppose you have a list of words and you want to sort them from longest to shortest: + +def sort_by_length(words): + t = [] + for word in words: + t.append((len(word), word)) + + t.sort(reverse=True) + + res = [] + for length, word in t: + res.append(word) + return res + +t = [] +for i in l: + t.append((len(i),i)) +print t + + + + + From 63ee86a38df248c427274cf4afe18f32e0760a31 Mon Sep 17 00:00:00 2001 From: Chris L Buie Date: Sun, 21 Aug 2016 14:42:25 -0500 Subject: [PATCH 13/13] update of tuples notes --- code/cb_tuples.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/code/cb_tuples.py b/code/cb_tuples.py index d966f65..2922c92 100644 --- a/code/cb_tuples.py +++ b/code/cb_tuples.py @@ -97,5 +97,7 @@ def sort_by_length(words): +import sys +sys.path.append( '/Users/chrisbuie/PycharmProjects/ThinkPython/code')