Dictionaries - University Of Washington

3y ago
26 Views
2 Downloads
281.48 KB
12 Pages
Last View : 28d ago
Last Download : 3m ago
Upload by : Fiona Harless
Transcription

DictionariesRob ThompsonUW CSE 160Winter 20211

Dictionaries or mappings 7 49A dictionary maps each key to a valueOrder does not matterGiven a key, can look up a value– Given a value, cannot look up its key No duplicate keys5 256 367 496 365 2549 77 4949 -7-7 49– Two or more keys may map to the same value Keys and values are Python values1783 “Revolutionary”– Keys must be immutable (not a list, set, or dict) Can add key value mappings to a dictionary1848 “Mexican”1865 “Civil”– Can also remove (less common)“Revolutionary” 1775 1783“Mexican” 1846 1848“Civil” 1861 1865“WWI” 1917 1918addmapping“Revolutionary” 1775 1783“Mexican” 1846 1848“Civil” 1861 18652

See in python tutorDictionary syntax in Pythond {}d dict()Two different waysto create an emptydictionary1783 “Revolutionary”us wars by end {1848 “Mexican”1783: "Revolutionary",1865 “Civil”1848: "Mexican",1865: "Civil" }us wars by name {“Revolutionary” 1775 1783"Civil": [1861, 1865],"Mexican": [1846, 1848],“Mexican” 1846 1848"Revolutionary": [1775, 1783]“Civil” 1861 1865} Syntax just like lists, for accessing and setting:us wars by end[1783] us wars by end[1783][1:10] us wars by name["WWI"] [1917, 1918]3

See in python tutorCreating a dictionary“GA” “Atlanta”“WA” “Olympia” state capitals {"GA" : "Atlanta", "WA": "Olympia" } phonebook dict() phonebook["Alice"] "206-555-4455" phonebook["Bob"] "212-555-2211" “Alice” “206-555-4455”atomic number {}atomic number["H"] 1atomic number["Fe"] 26atomic number["Au"] 79“Bob” “212-555-1212”“H” 1“Fe” 26“Au” 794

See in python tutorAccessing a dictionary“H” 1 atomic number {"H":1, "Fe":26, "Au":79} atomic number["Au"]“Fe” 2679“Au” 79 atomic number["B"]Traceback (most recent call last):File " pyshell#102 ", line 1, in module atomic number["B"]KeyError: 'B' "Au" in atomic numberTrueGood for iteration (for loops) list(atomic number.keys())for key in mymap.keys():['H', 'Au', 'Fe']val mymap[key] list(atomic number.values()) use key and val[1, 79, 26]for key in mymap: list(atomic number.items())val mymap[key][('H', 1), ('Au', 79), ('Fe', 26)] use key and valfor (key,val) in mymap.items(): use key and val5

See in python tutorIterating through a dictionaryatomic number {"H":1, "Fe":26, "Au":79}# Print out all the keys:for element name in atomic number.keys():print(element name)# Another way to print out all the keys:for element name in atomic number:print(element name)# Print out all the values:for element number in atomic number.values():print(element number)# Print out the keys and the valuesfor (element name, element number) in atomic number.items():print("name:", element name, "number:", element number)6

See in python tutorModifying a dictionaryus wars1 {"Revolutionary": [1775, 1783],"Mexican": [1846, 1848],"Civil": [1861, 1865] }us wars1["WWI"] [1917, 1918] # add mappingdel us wars1["Civil"] # remove mapping“Revolutionary” 1775 1783“Mexican” 1846 1848“Civil” 1861 1865“WWI” 1917 1918addmapping“Revolutionary” 1775 1783“Mexican” 1846 1848“Civil” 1861 18657

See in python tutorDictionary Exercises What does this do?squares {1: 1, 2: 4, 3: 9, 4: 16}squares[3] squares[3]squares[3 3]squares[2] squares[2]squares[2 2] Convert a list to a dictionary: Given [5, 6, 7], produce {5: 25, 6: 36, 7: 49} Reverse key with value in a dictionary:– Given {5:25, 6:36, 7:49}, produce {25:5, 36:6, 49:7}8

See in python tutorDictionary Exercise (Answers) Convert a list to a dictionary:– E.g. Given [5, 6, 7], produce {5: 25, 6: 36, 7: 49}d {}for i in [5, 6, 7]:d[i] i * i# or range(5, 8) Reverse key with value in a dictionary:– E.g. Given {5: 25, 6: 36, 7: 49}, produce {25: 5, 36: 6, 49: 7}k {}for i in d.keys():k[d[i]] i9

Aside: A list is like a dictionary A list maps an integer index to a value– The integers must be a continuous range 0.imylist ['a', 'b', 'c']mylist[1] 'b'mylist[3] 'c' # error! In what ways is a list more convenient than a dictionary? In what ways is a list less convenient than a dictionary?10

Not every value is allowed to be a keyin a dictionary Keys must be immutable values– int, float, bool, string, tuple of immutable types– not: list, set, dictionary The dictionary itself is mutable (e.g. we canadd and remove elements)11

Not every value is allowed to be a key Keys must be immutable values– int, float, bool, string, tuple– not: list, set, dictionary Goal: only dictionary operations change the keyset Mutable keys can violate these goals– after “mydict[x] y”, mydict[x] y– if a b, then mydict[a] mydict[b]These conditions should hold until mydict itself is changedlist1 ["a", "b"]list2 list1list3 ["a", "b"]mydict {}mydict[list1] "z" Hypothetical; actually illegal in Pythonmydict[list3] "z"list2.append("c")mydict[list1] ?mydict[list3] ?12

Dictionaries or mappings A dictionary maps each key to a value Order does not matter Given a key, can look up a value –Given a value, cannot look up its key No duplicate keys –Two or more keys may map to the same value Keys and values are Python values –Keys must be immutable (not a list, set, or dict)

Related Documents:

Dictionaries! Dictionaries are Pythonʼs most powerful data collection! Dictionaries allow us to do fast database-like operations in Python! Dictionaries have different names in different languages! Associative Arrays - Perl / Php! Properties or Map or HashMap - Java! Property Bag - C# / .Net!

The AUPN Package contains all dictionaries that comprise the ‘clinical repository’ or Patient Care Component (PCC). The dictionaries in this packageare the primary files in which patient medical and registration data is housed. This package also contains various utilities and routines called from these dictionaries.

The American heritage guide to contemporary usage and style, p. cm. ISBN-13: 978--618-60499-9 ISBN-10: -618-60499-5 1. English language—Usage—Dictionaries. 2. English language—Style—Dictionaries. 3. English language—United States—Usage—Dictionaries. 4. English language—United States—Style—Dictionaries. I. Houghton Mifflin .

IJL Dictionaries: usefulness and usability 4 seen dictionaries as an essential contribution to work on endangered languages, and as such they have been mainly concerned with the task of preserving languages for future study or revival. In the past the main audience for dictionaries of these languages has been felt to be linguists and other

AdvanceDataStructures-Unit-1(Dictionaries) Page4of 38 Unit-I Dictionaries SET:-A set is a collection of welldefinedelements.The members ofasetare alldifferent. A set is a group of "objects" People in a class: { Alice, Bob, Chris } Classes offered by a department: { CS 101, CS 202,

the cost of creating new dictionaries in the first place may be pro-hibitively expensive and time-consuming for many researchers. For example, many of the LIWC dictionaries used up to 8 judges across several stages in order to manually expand the word lists (Pennebaker et al., 2015). Given that the constructs measured by existing dictionaries are

dictionaries work by ensuring that encoded keys maintain the same order as values (e.g. for mappings k1 v1 and k2 v2, k1 k2 iff v1 v2). Note that we refer to order-preserving dictionaries as OP for short. Generating the mapping to support OP dictionaries comes at a cost. If the code domain is a finite domain, such as 32-

dictionaries as a feature when extracting parallel phrases from comparable corpora and report mistranslated pairs of phrases mainly due to noise in the statistical dictionaries. Although the authors clean their dictionaries by removing every entries that have lower probability values than a man-ually determined threshold, their results show .