Question

Dictionaries

1)
You are given a variable, wwII_battle_winners , that is associated with a dictionary that maps the names of World War II battles to the victors of WWII battles. Writesome code that associates a sorted list of the battle names that appear as keys in this dictionary with the variable battles .

2)

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 . Associate the new map with the variable inverse . You may assume that there areno duplicate values in d (that is no two keys in d map to the same value).

3)
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 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 {2:9, 6:3}
Associate the new dictionary with the variable d3

4)

Given a dictionary d and a list lst, remove all elements from the dictionary whose key is an element of lst. For example, given the dictionary {1:2, 3:4, 5:6, 7:8} andthe list [1, 7], the resulting dictionary would be {3:4, 5:6}.
Assume every element of the list is a key in the dictionary.
1 0
Add a comment Improve this question Transcribed image text
Answer #1

Program Screenshots:

# declare the dictionary ww11 battle winners { india : winner!, china : winner3, russia : winner2., # assign k

Sample output:

[ china, india,russia]

Code to Copy:

# Use python 3.5 or 3.6

# indentation of the program should be same as provided in the above screenshot:

# declare the dictionary
wwII_battle_winners = {'india':'winner1','china':'winner3','russia':'winner2'}

# assign keys to variable battles.
battles = wwII_battle_winners.keys()

# sort the keys
battles=sorted(battles)

# Display the keys on console.
print (battles)

Add a comment
Answer #2

Answer

battles = list(wwII_battle_winners.keys())
battles.sort()

below is the complete program with testinq if you are interested

wwII_battle_winners = {
    "b1": "v1",
    "b5": "v2",
    "b2": "v3",
    "b9": "v4",
}

battles = list(wwII_battle_winners.keys())
battles.sort()

print(battles)

Process finished with exit code 0

Add a comment
Answer #3
2 ans:
 inverse = dict(zip(d.values(), d.keys())) 
Add a comment
Answer #4
4) ans:

SOURCE CODE:

def remover(d,lst):
for i in lst:
d.pop(i,None)
return d
  

1 def remover(d,lst): for i in lst: d.pop (i,None) return d 2 4 1:2, 345:6, 7:8 9 print(remover(d,l)) 10 11 d- [1:2, 3:4, 5:6

Add a comment
Know the answer?
Add Answer to:
Dictionaries
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Given dictionaries, d1 and d2, create a new dictionary with the following property: for each entry (a, b) in d1, if t...

    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 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 {2:9, 6:3} Associate the new dictionary with the variable d3

  • Phython read_file (fp)  D1,D2,D3: a) This function read a file pointer and returns 3 dictionaries:...

    Phython read_file (fp)  D1,D2,D3: a) This function read a file pointer and returns 3 dictionaries: - Parameters: file pointer (fp) - Returns : 3 dictionaries of list of tuples - The function displays nothing b) You must use the csv.reader because some fields have commas in them. c) Each row of the file contains the name of a video game, the platform which it was released, the release year, the genre (i.e. shooter, puzzle, rpg, fighter, etc.), the publishing...

  • Write a function called, sameKeys, that takes in two dictionaries. The sameKeys function looks for keys...

    Write a function called, sameKeys, that takes in two dictionaries. The sameKeys function looks for keys that are found in both dictionaries and returns a new dictionary that contains key:value pairs where the key in the new dictionary is the key found in both dictionaries, dictionl and diction2, and the new key's value being a list of the values of dictionl.key and diction2.key values concatenated together. Assumptions 1) 2) Both dictionaries can be empty and an empty dictionary is returned...

  • Dictionaries

    1)Assume there is a variable, nobel_peace_prizes , that is associated with a dictionary that maps years to winners of the Nobel Peace Prize for that year and assume itis up-to-date through the year 2005. Write a statement that adds an entry that maps the key 2006 to "Muhammad Yunus and Grameen Bank" .2)Given a variable, us_cabinet , that is associated with a dictionary that maps department names to department heads, replace the value "Gonzalez" with "Mukasey" for thekey "Justice Department"3)The...

  • Updating a Dictionary When working with lists, you probably noticed that you can change the value...

    Updating a Dictionary When working with lists, you probably noticed that you can change the value at a given index with a simple assignment statement. So, for example, the following code replaces the first element of lst with 42: lst[0] = 42 You can update the value for a given key in a dictionary in the same way. The general syntax for this is: d[key] = value This is also how you insert new key/value pairs to a dictionary; if...

  • Storing data and adding to data in a dictionary: Experiment with Python dictionaries and their keys()...

    Storing data and adding to data in a dictionary: Experiment with Python dictionaries and their keys() member function that returns a list of a dictionary's keys. You can check if a particular key is in a dictionary by using the in test with the keys list. For example, if our dictionary is MyDict = { 1: 'One', 2: 'Two', 3: 'Three' } Then ( 2 in MyDict.keys() ) evaluates to True, indicating that 2 is a valid key for MyDict....

  • Define the functions in Python 3.8 1. Write a function most frequent n that takes a...

    Define the functions in Python 3.8 1. Write a function most frequent n that takes a list of strings and an integer n, and that returns a dictionary where the keys are the top n most frequent unique words in the list, and the values are the frequency of each word: For example, most frequent n(text, 3) should return the dictionary {'is': 2, "the’: 3, 'of': 2}, and most frequent n(text, 2) could return either {'is': 2, 'the’: 3} or...

  • 1. use python List to Dictionary Write a function that has three parameters: a list of...

    1. use python List to Dictionary Write a function that has three parameters: a list of unsorted numbers with no duplicates, a start number, and an end number. This function should return a dictionary with all integers between the start and end number (inclusive) as the keys and their respective indices in the list as the value. If the integer is not in the list, the corresponding value would be None. Example unsorted list: [2,1,10,0,4,3] two numbers: 3, 10 returned...

  • Python 3 Define a function below, get_subset, which takes two arguments: a dictionary of strings (keys)...

    Python 3 Define a function below, get_subset, which takes two arguments: a dictionary of strings (keys) to integers (values) and a list of strings. All of the strings in the list are keys to the dictionary. Complete the function such that it returns the subset of the dictionary defined by the keys in the list of strings. For example, with the dictionary ("puffin": 5, "corgi": 2, "three": 3) and the list ("three", "corgi"), your function should return {"corgi": 2, "three"...

  • Python 3.7.3 ''' Problem 3 Write a function called names, which takes a list of strings...

    Python 3.7.3 ''' Problem 3 Write a function called names, which takes a list of strings as a parameter. The strings are intended to represent a person's first and last name (with a blank in between). Assume the last names are unique. The function should return a dictionary (dict) whose keys are the people's last names, and whose values are their first names. For example: >>> dictionary = names([ 'Ljubomir Perkovic', \ 'Amber Settle', 'Steve Jost']) >>> dictionary['Settle'] 'Amber' >>>...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT