0% found this document useful (0 votes)
27 views2 pages

Learning Journal Unit 7

The document is a learning journal that contains: 1) A dictionary with personal information like weight, height and field of study for 7 people. 2) A function that inverts the dictionary to map values in the original dictionary to keys in the new inverted dictionary since values can be non-unique. 3) The inverted dictionary prints each value from the original dictionary mapped to a list of corresponding keys.

Uploaded by

safia yaqubi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views2 pages

Learning Journal Unit 7

The document is a learning journal that contains: 1) A dictionary with personal information like weight, height and field of study for 7 people. 2) A function that inverts the dictionary to map values in the original dictionary to keys in the new inverted dictionary since values can be non-unique. 3) The inverted dictionary prints each value from the original dictionary mapped to a list of corresponding keys.

Uploaded by

safia yaqubi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Learning Journal Unit 7

Hello dear instructor

Here is my journal assignment.

# name : [weight, hight, field]

f={

"Frozan": [50, 1.60, 'civil engineering'],

"Frahnaz": [51, 1.62, 'transportation engineering'],

"Sakina": [56, 1.57, 'electrical engineering'],

"Nasiba": [53, 1.63, 'architecture'],

"Muhammad": [68, 1.80, 'computer science'],

"Ahmad": [72, 1.80, 'civil engineering'],

"Sadaf": [51, 1.57, "architecture"]

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]

for item in val:

if item not in inverse:

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.

You might also like