python from introduction to practice Chapter 6 exercises (Advanced Programming Technology week3-2)

python from introduction to practice Chapter 6 exercises (Advanced Programming Technology week3-2)

This lesson is mainly about the use of dictionary data structure in python.

Basic use of dictionary

6-1 person

dict={
    "first_name" : "Walker",
    "last_name" : "Mike",
    "age" : "18",
    "city" : "New York",
}

print('the first name is ', dict['first_name'])
print('the last name is ', dict['last_name'])
print('the age is ', dict['age'])
print('the city is ', dict['city'])

6.3 traversal dictionary

The related list of dictionaries can be generated by the following functions, and for can be used to iterate through the entire dictionary:
1. dict.items(): returns a list of key value pairs.
1. dict.keys(): returns a list containing only keys.
1. dict.values(): returns a list of values only.

6-5 rivers

rivers = {
    'nile':'egypt',
    'Yellow River':'china',
    'Yangtze River':'china',
}
for river,country in rivers.items():
    print('The ', river.title(), ' runs through Egypt.')

for river in rivers.keys():
    print('The name of river is ',river.title())

for country in rivers.items():
    print('The country is ', country.title())

6-6 survey

favorite_languages = {
      'jen': 'python',
      'sarah': 'c',
      'edward': 'ruby',
      'phil': 'python',
      }
users = ['jen','john','ergou','mike','sarah','edward','phil']

print("The following languages have been mentioned:")
for name in users:
    if name in favorite_languages.keys():
        print('Thank you, ' + name.title())
    else:
        print('Would you participate in the survey?')

6.4 nesting

Dictionaries, nesting of lists.

  1. Store dictionary in list
  2. Store list in dictionary
  3. Store dictionary in dictionary

6-7 person

walker={
    "first_name" : "Walker",
    "last_name" : "Mike",
    "age" : "18",
    "city" : "New York",
}

john={
    "first_name" : "John",
    "last_name" : "Mike",
    "age" : "19",
    "city" : "New York",
}

lily={
    "first_name" : "lily",
    "last_name" : "Mike",
    "age" : "17",
    "city" : "New York",
}

people = [walker, john, lily]

for person in people:
    print(person['first_name'].title(), persion['last_name'].title(), ' is ', persion['age'], ' now living in ', person['city'], '.')

6-9 favorite places

favorite_places = {
    'john':['beijing', 'hang zhou'],
    'mike':['guang zhou'],
    'lily':['si chuang'],
}

for person, places in favorite_places.item():
    print(person.title(), ' likes these places:')
    for place in places:
        print('|',place)

6-11

cities = {}
cities['Guang Zhou'] = {
    'country':'china',
    'population':82342523,
    'fact':'This city is beautiful!',
}

cities['Hang Zhou'] = {
    'country':'china',
    'population':12982415,
    'fact':'This city is beautiful, too!',
}

cities['Beijing'] = {
    'country':'china',
    'population':129832415,
    'fact':'This city is big and beautiful!',
}

for city_name in cities.keys():
    for arg, content in cities[city_name].items():
        print('The ', arg, ,' of ', city_name, ' is ', content, ' .')

6.5 summary

This chapter has learned some simple ways to use dictionaries.

Tags: Python Programming Ruby

Posted on Tue, 31 Mar 2020 06:43:19 -0700 by scratchwax2003