Home > Software design >  how to match the keys of a dictionary and return their values?
how to match the keys of a dictionary and return their values?

Time:03-14

I have two dictionaries:

x= {'албански': 'Albanian', 'елзаски': 'Alsatian', 'арагонски': 'Aragonese', 
'арберешки': 'Arberesh'}

y={'елзаски': 477, 'арагонски': 0, 'арберешки': 1,'албански': 1}

Both dictionaries contains the same amount of key_value pairs. In dictionary x, the key is the translation in Bulgarian of names of languages and the values are the names of the same languages in English.

in dictionary y, the keys are the name of the languages in Bulgarian and the values are their frequency counts.

All languages present in Dic x are present in dic y, but they are in different orders.

What I need to do is to return the names of the languages in English and their corresponding counts. For this I first need to search for the keys in dic x in dic y and when they match return the values in x and values in y. How to do this?

I have tried the following code, but does not work and even if it worked I think I would not get what I need.

x=dict(zip(lang_lists['languages_bulgarian'],lang_lists['languages_english']))
y=dict(zip(lang_lists['report_lang_list'], lang_lists['counts']))

for i in x:
  for j in y:
    if j == y:
      print(x[i], y[j])
    else:
      pass

My idea was to search the corresponding keys between the two dictionarys and when they match return the values of one dictionary (languages in English) and the values in the other dictionary (frequency counts). my desired output is something like:

{Albanian: 1, Aragonese: 0, Arberesh: 1, Alsatian: 477}

CodePudding user response:

using dict comprehension

x = {'албански': 'Albanian', 'елзаски': 'Alsatian', 'арагонски': 'Aragonese', 
'арберешки': 'Arberesh'}

y = {'елзаски': 477, 'арагонски': 0, 'арберешки': 1,'албански': 1}

result = {value:y.get(key) for key, value in x.items()}
print(result)

output

{'Albanian': 1, 'Alsatian': 477, 'Aragonese': 0, 'Arberesh': 1}

CodePudding user response:

While the dict comprehension buran's answer is neat, the key thing here is that you can loop over dictionaries.

In the "natural" way you get the keys in such loop:

x = {'албански': 'Albanian', 'елзаски': 'Alsatian', 'арагонски': 'Aragonese', 
'арберешки': 'Arberesh'}
y = {'елзаски': 477, 'арагонски': 0, 'арберешки': 1,'албански': 1}

z = {}
for key in x:
  z[x[key]] = y[key]

print(z)

And it's also possible to get key-value pairs directly, using items():

for key,value in x.items():
  z[value] = y[key]

CodePudding user response:

You can loop through two items together with for rather than using nested loop and zip will pair the values with the same key.

x= {'албански': 'Albanian', 'елзаски': 'Alsatian', 'арагонски': 'Aragonese', 
'арберешки': 'Arberesh'}

y={'елзаски': 477, 'арагонски': 0, 'арберешки': 1,'албански': 1}

temp = dict()
for (key1, value1), (key2,value2) in zip(x.items(), y.items()):
    temp[value1] = y[key1] # Add this
    
print temp

Output:

{'Albanian': 1, 'Alsatian': 477, 'Aragonese': 0, 'Arberesh': 1}

EDIT:

This is the simplest way:

x= {'албански': 'Albanian', 'елзаски': 'Alsatian', 'арагонски': 'Aragonese', 
    'арберешки': 'Arberesh'}
    
y={'елзаски': 477, 'арагонски': 0, 'арберешки': 1,'албански': 1}

temp = dict()
for (key, value) in x.items():
    temp[value] = y[key]
    
print(temp)
  • Related