Exercises 38
Code
ten_things = "Apples oranges Crows Telephone light Sugar" print "Wait there is not the things in the that list. Let's fix that. " Stuff = Ten_things.split (') More_stuff = ["Day", "Night", "Song", "Frisbee", "Corn", "Banana", "Girl", "Boy"]while Len (St UFF)! = 10:next_one = More_stuff.pop () print "Adding:", Next_onestuff.append (next_one) print "There is%d items now."% le N (stuff) print "There we go:", stuff print "Let's do some things with stuff." Print Stuff[1]print stuff[-1] # whoa! Fancyprint Stuff.pop () print ". Join (Stuff) # What? Cool!print ' # '. Join (Stuff[3:5]) # Super stellar!
Output
Notes:
Application of ①split () function, join () function
Slicing operations for ② lists
Exercise 39
Code
Output
The interpretation of the principle of hashmap.py dictionary
Def new (num_buckets=256): "initializes a map with the given numbers Of buckets. " Amap = []for i in range (0, num_buckets): Amap.append ([]) return aMapdef Hash_key (Amap,key): "given a key this will create a number and Then convert it toan index for the amap ' S buckets. ' Return hash (Key) % len (AMAP) Def get_bucket (amap, key): "given a key, Find the bucket where it would go. " Bucket_id = hash_key (Amap, key) return amap[bucket_id]def get_slot (AMap, key, default=none): "Returns the index, key, and value of a slot found in a bucket. returns -1, key, and default (None if not set) when not foune. " bucket = get_bucket (Amap, key) for i, kv in enumerate (bucket): k, v = kv if key == k:return i, k, v return -1, key, default def get (Amap, key, default=none): "Gets the value in a bucket for the given key, or the default ' I, k, v = get_slot (Amap, key, default = default) Return v def set (aMap, key, value): "' Sets the key to the value, replacing any existing value. ' Bucket = get_bucket (Amap. key) i, k, v = get_slot (AMap, key) if i >= 0:# the key exits, replace it bucket[i] = (key, Value) Else:# the key does not, append to create it.bucket.append (key, Value) Def delete (AMAp, key): "Deletes the given key from the map." Bucket = get_bucket (Amap, key) For i in xrange (len): k, v = bucket[i]if key == k:del bucket[i]break def list (AMAP): ' Prints out what ' S in the map. for bucket in amap: if bucket: for k, v in bucket: print k, v
Additional code/small experiment ex39_test.py
import hashmap# create a mapping of state to abbreviationstates = hashmap.new () hashmap.set (states, ' Oregon ', ' OR ') hashmap.set (states, "Florida", ' FL ') Hashmap.set (states, ' California ', ' CA ') hashmap.set (states, ' new york ', ' NY ') Hashmap.set (states, ' Michigan ', ' MI ') # create a basic set of states and some cities in themcities = hashmap.new () hashmap.set (cities, ' CA ' , ' San francisco ') hashmap.set (cities, ' MI ', ' Detroit ') hashmap.set (cities, ' FL ', ' Jacksonville ') # add some more cities hashmap.set (cities, ' NY ', ' New York ') hashmap.set (cities, ' OR ', ' Portland ') # print out some cities print '-' * 10print ' ny state has: %s ' % hashmap.get (cities, ' NY ' ) print "Or state has: %s " % hashmap.get (cities, ' OR ') # print some states print "-" * 10print "Michigan ' s abbreviation is: %s" % hashmap.get ( states, ' Michigan ') print "Florida ' s abbreviation is: %s" % hashmap.get ( states, ' Florida ') # do it by using the state then cities dict print "-" * 10print "michigan has: %s" % hashmap.get (cities, hashmap.get (states, ' Michigan ')) print "florida has: %s" % hashmap.get ( Cities, hashmap.get (states, ' Florida ')) # print every state abbreviation print "-" * 10hashmap.list (states) # print every city in stateprint "-" * 10hashmap.list (cities) print "-" * 10state = hashmap.get (states, ' Texas ') If not state: print "Sorry, no texas" # default Values useing //= with the nil result # can you do this on one line?city = hashmap.get (cities, ' TX ', ' Does Not Exist ') print "the city for the state ' TX ' is: %s" % city # Yes, I can do this on one line.print ' The city for the state \ ' tx\ ' is: %s ' % hashmap.get (cities, ' TX ', ' Does Not exist ')
Output
Notes:
The ① list is ordered, and the dictionary is a map of key and value and is unordered
The items () method of the ② dictionary, which returns a tuple that includes a pair of Key-value;get (Key[,return]) that returns the value corresponding to the key, if the key does not exist and the return value is set, returns the return value, otherwise none
The enumerate () function of the ③ list, which makes it easy to get index and index-pointing elements when iterating
The ④hash () function is a hash function that converts an object to a number, a different object, and a different number.
Stupid methodology python, Lesson 38, 39