《Python编程:从入门到实践》字典嵌套实践

《Python编程:从入门到实践》字典嵌套实践_第1张图片
6-7

Sunjichen = {
    "first_name" : "Sun",
    "last_name" : "jichen",
    "city" : "Dezhou",
    "age" : "19",
    }
Lihanpeng = {
    "first_name" : "Hanpeng",
    "last_name" : "Li",
    "city" : "Yantai",
    "age" : "20",
    }
Xiaominghao = {
    "first_name" : "Minghao",
    "last_name" : "Xiao",
    "city" : "Taian",
    "age" : "19",
    }
people = [Sunjichen, Lihanpeng, Xiaominghao]
print(people)

6-8

kitty = {
    'type' : 'dog',
    'master' : 'Tom',
    }

bob = {
    'type' :'cat',
    'master' : 'Jim',
    }

rocket = {
    'type' : 'turtle',
    'master' : 'Sam',
    }

pets = [kitty,bob,rocket]
print(pets)

6-9

favourite_places = {
    'Sunjichen' : ["Xian","Shanghai","beijing"],
    'Lihanpeng' : ["Tianjin","Yantai"],
    'Xiaominghao' : ["Taipei"],
    }
for name, places in favourite_places.items():
    if len(places) > 1:
        print("\n" + name + "`s favourite places are:")
        for place in places:
            print("\t" + place)
    else:
        print("\n" + name + "`s favourite place is:")
        print("\t" + place)

6-10

favourite_numbers = {
    'sunjichen' : ['9','10','11'],
    'lihanpeng' : ['8','17','222'],
    'xiaominghao' : '7',
    'zhangenbo' : '6',
    'zhoutao' : '5',
    }
for name,numbers in favourite_numbers.items():
    if len(numbers) > 1 :
        print("\n" + name + " `s favourite numbers are:")
        for number in numbers:
            print("\t" + number)
    else :
        print("\n" + name + " `s favourite numbers is:")
        print("\t" + numbers)

6-11

cities = {
    'beijing' : {
        'country' : 'china',
        'population' : '21540000',
        'fact' : 'capital of China',
        },
    
    'tokyo' : {
        'country' : 'japan',
        'population' : '13290000',
        'fact' : 'capital of Japan',
        },

    'new york' : {
        'country' : 'america',
        'population' : '8510000',
        'fact' : 'a big city',
        },
    }

for city,info in cities.items():
    print("\n" + city.title())
    print(city.title() + " is in " + info['country'].title())
    print(city.title() + " has a population of " + info['population'])
    print(city.title() + " is " + info['fact'])

你可能感兴趣的:(《Python编程:从入门到实践》字典嵌套实践)