第6章 字典

6-1人:

#6-1
message ={'first_name':'wang','last_name':'xiaomao','age':24,'city':'shenzhen'}
print(message['first_name'].title()+message['last_name']+' '+str(message['age'])+'years old,'
      +'and live in '+message['city']+'.')

6-2喜欢的数字:

#6-2
favorite_nums={
    'tom':3,
    'mom':4,
    'dad':5,
    'tim':6,
    'ken':0,}
print('Tom favorite numbers is '+str(favorite_nums['tom']))
print('Mom favorite numbers is '+str(favorite_nums['mom']))
print('Dad favorite numbers is '+str(favorite_nums['dad']))
print('Tim favorite numbers is '+str(favorite_nums['tim']))
print('Ken favorite numbers is '+str(favorite_nums['ken']))

6-3 词汇表:

messages={
     'first_name':'Tom',
     'last_name':'Smith',
     'age':25,
     'city':'Beijing'}
for key,value in messages.items():
     print(key,':',value)

6-4词汇表2:

#6-4
messages={
     'first_name':'Tom',
     'last_name':'Smith',
     'age':25,
     'city':'Beijing'}
for key,value in messages.items():
     print(key,':',value)
print('\n')
for key in messages.keys():
    print(key.title())
print('\n')
for value in messages.values():
    print(value)

6-5河流:

#6-5
rivers = {'nile':'egypt',
          'huanghe':'china',
          'changjiang':'china',}
for key,value in rivers.items():
    print('The '+key+' run through '+value)
for key in rivers.keys():
    print(key)
for value in rivers.values():
    print(value)

6-6调查:

#6-6
favorite_languages ={
    'jen':'python',
    'sarch':'c',
    'edward':'ruby',
    'phil':'python',
    }
for name,language in favorite_languages.items():
    print(name.title()+"'s favorite language is "+
          language.title()+'.')
print('\n')
list_1 = {'jen','sarch'}
for list in favorite_languages.keys():
    if list in list_1:
        print(list.title()+' thank you .')
    else:
        print(list.title()+' would you like to attend this research.')

6-7人:

#6-7
message ={
    'man_1':{
        'first_name':'wang','last_name':'xiaomao','age':24,'city':'shenzhen'},
    'man_2':{
        'first_name':'chen','last_name':'ming','age':24,'city':'guangzhou'},
    'man_3':{
        'first_name':'zhang','last_name':'san','age':22,'city':'beijing'},
    }
for username,user_info in message.items():
    full_name =user_info['first_name']+user_info['last_name']
    age=user_info['age']
    location= user_info['city']
    print(full_name.title()+', '+str(age)+'years old,'+'live in '+location.title())

6-8宠物:

#6-8
pets_0={'kind':'cat','host_name':'tom'}
pets_1={'kind':'dog','host_name':'tim'}

pets=[pets_0,pets_1]
for pet in pets:
    print(pet)

6-9喜欢的地方:

#6-9
favorite_places={
    'tom':'taiwan,xiamen,changsha',
    'tim':'wuhan,beijing',
    'jun':'guangzhou,nanjing',}
for key,value in favorite_places.items():
    print(key.title()+' favorite place are: '+value.title())

6-10喜欢的数字:

#6-10
favorite_nums={
    'tom':[3,5],
    'mom':[4,7],
    'dad':[5,9],
    'tim':[6,1,2],
    'ken':[0,4]}
for user,num in favorite_nums.items():
    print(user.title()+'喜欢的数字是: '+str(num))
    

 6-11城市:

#6-11
cities={
    'beijing':{
        'country':'china',
        'population':'most',
        'fact':'beautiful',
        },
    'newyork':{
        'country':'america',
        'population':'most',
        'fact':'rich',
        },
    }
for city_name,city_info in cities.items():
    print('\n'+city_name.title())
    country=city_info['country']
    population=city_info['population']
    fact=city_info['fact']

    print(':country is '+country.title()
          +', population '+population
          +' and fact '+str(fact))

  

  

posted @ 2020-05-21 14:32  猫山思  阅读(125)  评论(0)    收藏  举报