08-03-04 Dictionaries - dctionary-traversal

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

You are given a variable, wwii_battle_winners, that contains a dictionary that maps the names of World War II battles to the victors of WWII battles. Write some code that assigns a sorted list of the battle names that appear as keys in this dictionary to the variable battles.

battles = sorted(wwii_battle_winners)

Given dictionaries, d1 and d2, create a new dictionary with the following property: For each entry (a, b) in d1, if a is not a key of d2 (i.e., not a in d2) then add (a,b) to the new dictionary. For each entry (a, b) in d2, if a is not a key of d1 (i.e., not a in d1) then add (a,b) to the new dictionary. For example, if d1 is {2:3, 8:19, 6:4, 5:12} and d2 is {2:5, 4:3, 3:9}, then the new dictionary should be {8:19, 6:4, 5:12, 4:3, 3:9} Assign the new dictionary to the variable d3.

d3 = {} for a,b in d1.items() : [Tab Key]if a not in d2: [Tab Key][Tab Key]d3[a] = d1[a] for a,b in d2.items() : [Tab Key]if a not in d1: [Tab Key][Tab Key]d3[a] = d2[a]

Given dictionaries, d1 and d2, create a new dictionary with the following property: for each entry (a, b) in d1, if there is an entry (b, c) in d2, then the entry (a, c) should be added to the new dictionary. For example, if d2 is {2:3, 8:19, 6:4, 5:12} and d2 is {2:5, 4:3, 3:9}, then the new dictionary should be {2:9, 6:3} Assign the new dictionary to the variable d3.

d3 = {} for a,b in d1.items() : [Tab Key]if b in d2.keys() : [Tab Key][Tab Key]d3[a] = d2[b]

The inverse of a map is a new map where the values of the original map become the keys of the new map and the keys become the values. For example, given the map {1:2, 3:4}, the inverse is the map {2:1, 4:3}. Given the map, d, create the inverse of d. Assign the new map to the variable inverse. You may assume that there are no duplicate values in d (that is no two keys in d map to the same value).

inverse = {} for k in d.keys() : [Tab Key]v = d[k] [Tab Key]inverse[v] = k

Given a dictionary d, create a new dictionary that reverses the keys and values of d. Thus, the keys of d become the values of the new dictionary and the values of d become the keys of the new dictionary. You may assume d contains no duplicate values (that is, no two keys map to the same values.) Assign the new dictionary to the variable inverse.

inverse = {} for key in d.keys() : [Tab Key]val = d[key] [Tab Key]inverse[val] = key

Given the dictionary, d, find the largest key in the dictionary and assign the corresponding value to the variable val_of_max. For example, given the dictionary {5:3, 4:1, 12:2}, 2 would be assigned to val_of_max. Assume d is not empty.

maxKey = list(d.keyss())[0] for k in d.keys() : [Tab Key]if k > maxKey : [Tab Key][Tab Key]maxKey = k val_of_max = d[maxKey]

Given a variable, polygon_sides, that contains a dictionary that maps names of polygons to number of sides, create a new dictionary that maps number of sides to polygon names, and assign it to a variable n_polygons.

n_polygons = {} for key in polygon_sides : [Tab Key]n_polygons[polygon_sides[key]] = key

Given three dictionaries, assigned to the variables, canadian_capitals, mexican_capitals, and us_capitals, that map provinces or states to their respective capitals, create a new dictionary that combines these three dictionaries, and assign it to a variable, nafta_capitals.

nafta_capitals = {} for key in us_capitals : [Tab Key]nafta_capitals[key] = us_capitals[key] for key in candian_capitals : [Tab Key]nafta_capitals[key] = canadian_capitals[key] for key in mexican_capitals : [Tab Key]nafta_capitals[key] = mexican_capitals[key]

Assume there is a variable, mp_affiliation, that contains a dictionary that maps the names of parliament members to party affiliations, assign to the variable party_size a dictionary that maps party names to the number of members they have.

party_size = {} for key in mp_affiliation : [Tab Key]if mp_affiliation[key] in party_size : [Tab Key][Tab Key]party_size[mp_affiliation[key]] += 1 [Tab Key]else : [Tab Key][Tab Key]party_size[mp_affiliation[key]] = 1

Given a variable, province_premier, that contains a dictionary that maps the province names to the names of province premiers, assign to premier_province a dictionary that is the inverse of province_premier, i.e. one that maps names of premiers to province names.

premier_province = {} for key in province_premier : [Tab Key]premier_province[province_premier[key]] = key

Given a variable, state_capitals, that contains a dictionary that maps U.S. states to their capitals, and another dictionary, provincial_capitals, that maps Canadian provinces to their capitals, assign a dictionary that maps states or provinces to their respective capitals with a variable, regional_capitals.

regional_capitals = {} for key in state_capitals : [Tab Key]regional_capitals[key] = state_capitals[key] for key in provincial_capitals : [Tab Key]regional_capitals[key] = provincial_capitals[key]

Given a variable, election_results, that contains a dictionary that maps candidate names to votes received, assign the name of the candidate with the most votes with the variable winner.

winner = None for key, votes in election_results.items() : [Tab Key]if winner == None or votes > max : [Tab Key][Tab Key]max = votes [Tab Key][Tab Key]winner = key


Ensembles d'études connexes

Texas Pre-License - Promulgated Contracts

View Set

Mental Health - Chapter 1 - Mental Health and Mental Illness

View Set

True or False Communism activity

View Set

Anatomy 231 Chapter 9 joints study guide

View Set

Chapter 10: PreTrial Activities and the Criminal Trial

View Set

Forensic Science Short Answer Review

View Set

Independent/ dependent variable notecards

View Set