56.6k views
5 votes
Dictionaries You are given a dictionary consisting of word pairs. Every word is a synonym the other word in its pair. all the words in the dictionary are different. after the dictionary there's one more word given . Find a synonym for him. Given a list of countries and cities of each country. then given the names of the cities. for each city specify thr country in which it is located.... python

2 Answers

3 votes

Answer:

Python answer:

dict = {}

times = int(input())

for _ in range(times):

a = input()

w1, w2 = a.split()

dict[w1] = w2

dict[w2] = w1

print(dict[input()])

Step-by-step explanation:

Intialize your dictionary.

Ask for how many keys you will be adding to the dictionary.

Over a for loop, take an input of keys and values and split them and add them to the dictionary.

I set the key as a value and a variable due to the fact that we are working with synonyms and if the user inputs a synonym that is only the value, we can't get another synonym out. Therefore it is better if we make each word a key and a value.

Then print a synonym of whatever the user inputs.

User Simon Nickerson
by
5.2k points
1 vote

Answer:

To add values to a dictionary, specify the dictionary name with the key name in quotes inside a square bracket and assign a value to it.

dictionary_name["key_name"] = value

Step-by-step explanation:

A python dictionary is an unindexed and unordered data structure. It's items a key-value pair separated by a colon and the entire dictionary items are enclosed in curly braces. Unlike a list, the dictionary does not use an index but uses the key to get or add a value to the dictionary.

User Fejd
by
5.1k points