diff --git a/code/cb_dictionary.py b/code/cb_dictionary.py new file mode 100644 index 0000000..27a3759 --- /dev/null +++ b/code/cb_dictionary.py @@ -0,0 +1,193 @@ +# General notes working with dictionaries from ThinkPython + +# create dictionary: + +x = dict() +x = {} + +print x + +# create empty dictionary of eng to spanish words: + +eng2sp = dict() + +# 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] + +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 + +''' +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) + diff --git a/code/cb_tuples.py b/code/cb_tuples.py new file mode 100644 index 0000000..2922c92 --- /dev/null +++ b/code/cb_tuples.py @@ -0,0 +1,103 @@ +''' +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) + +# 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 + + +# 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 + + + +import sys +sys.path.append( '/Users/chrisbuie/PycharmProjects/ThinkPython/code') + +