Learning Journal Unit 7
Learning Journal Unit 7
f={
print(f)
print("\n")
Output:
{'Frozan': [50, 1.6, 'civil engineering'], 'Frahnaz': [51, 1.62, 'transportation engineering'], 'Sakina': [56,
1.57, 'electrical engineering'], 'Nasiba': [53, 1.63, 'architecture'], 'Muhammad': [68, 1.8, 'computer
science'], 'Ahmad': [72, 1.8, 'civil engineering'], 'Sadaf': [51, 1.57, 'architecture']}
Description: A dictionary contains a collection of indices, which are called keys, and a collection of
values. In mathematical language, a dictionary represents a mapping from keys to values. Lists can
appear as values in a dictionary. For example, if you are given a dictionary that maps from letters to
frequencies (Downey, 2015). My dictionary is useful to know the personal information of mentioned
people.
def invert_dict(d):
inverse = dict()
for key in d:
val = d[key]
inverse[item] = [key]
else:
inverse[item].append(key)
return inverse
print(invert_dict(f))
Output:
{50: ['Frozan'], 1.6: ['Frozan'], 'civil engineering': ['Frozan', 'Frozan', 'Ahmad'], 51: ['Frahnaz'], 1.62:
['Frahnaz'], 'transportation engineering': ['Frahnaz', 'Frahnaz'], 56: ['Sakina'], 1.57: ['Sakina'], 'electrical
engineering': ['Sakina', 'Sakina'], 53: ['Nasiba'], 1.63: ['Nasiba'], 'architecture': ['Nasiba', 'Nasiba', 'Sadaf'],
68: ['Muhammad'], 1.8: ['Muhammad'], 'computer science': ['Muhammad', 'Muhammad'], 72:
['Ahmad']}
Description: Sometimes we might want to invert it; that is, create a dictionary that maps from
frequencies to letters. Since there might be several letters with the same frequency, each value in the
inverted dictionary should be a list of letters (Downey, 2015). The advantage of the invert dictionary is
this, to categories the same information of the mentioned people as a kay-value pair. Each time
through the loop, key gets a key from d and val gets the corresponding value (Downey, 2015).
Reference:
Downey, A. (2015). Think Python: How to think like a computer
scientist. Needham, Massachusetts: Green Tree Press.