字典嵌套

print("----")
alien_0 = {'color':'green', 'points':5}
alien_1 = {'color':'yellow', 'points':10}
alien_2 = {'color':'red', 'points':15}
aliens = [alien_0, alien_1, alien_2]
for alien in aliens:
    print(alien)

print('----')
aliens = []
for alien_number in range(30):
    new_alien = {'color':'green', 'points':5, 'speed':'slow'}
    aliens.append(new_alien)

for alien in aliens[:5]:
    print(alien)
print("...")
print("Total number of aliens: " + str(len(aliens)))

print('----')
aliens = []
for alien_number in range(30):
    new_alien = {'color':'green', 'points':5, 'speed':'slow'}
    aliens.append(new_alien)
for alien in aliens[:3]:
    if alien['color'] == 'green':
        alien['color'] = 'yellow'
        alien['speed'] = 'medium'
        alien['points'] = 10
        
for alien in aliens[:5]:
    print(alien)
print("...")

print('----')
pizza = {
    'crust':'thick',
    'topping':['mushrooms','extra cheese'],
}
print("You ordered a " + pizza['crust'] + '-crust pizza ' + 
    "with the following toppings:")
for topping in pizza['topping']:
    print("\t" + topping)
    
print('----')
favorite_languages = {
    'jen':['python', 'ruby'],
    'sarah':['c'],
    'edward':['ruby','go'],
    'phil':['python','haskell'],
}
for name, languages in favorite_languages.items():
    print('\n' + name.title() + "'s favorite languages are:")
    for language in languages:
        print("\t"+language.title())

print('----')
users = {
    'aeinstein':{
        'first':'albert',
        'last':'einstein',
        'location':'princeton',
    },
    'mcurie':{
        'first':'marie',
        'last':'curie',
        'location':'paris',
    },
}
for username, user_info in users.items():
    print("Username: " + username.title())
    full_name = user_info['first'] + " " + user_info['last']
    location = user_info['location']
    print("\tFull name: " + full_name.title())
    print("\tLocation: " + location.title())

print('====')
friends = {
    'ZhangYunyi': 0,
    'LiXia': 1,
    'LiuJun': 2,
    'XuYuchi': 3,
    'PanChunyun': 4,
}
school_mate = {
    'WuWenjie': 5,
    'ZhengJun': 6,
}
baby_friends = {
    'JinChangxi':7,
    'RiZhe':8,
}
people = [friends, school_mate, baby_friends]
for mans in people:
    for name,number in mans.items():
        print(name.title() + ": " + str(number))
        
print("====")
dog = {
    'type':'type_dog',
    'owner':'Jiefu',
    }
cat = {
    'type':'type_cat',
    'owner':'Jiejie',
}
pets = [dog, cat]
for pet in pets:
    for sType, sOwner in pet.items():
        print(sType + ":" + sOwner)
        
print("====")
favorite_places = {
    "ZhangYunyi":['Paris','Xian','Beijing','Bacilona'],
    "LiuJun":['London','Madrid'],
    "LiXia":['London','Bacilona'],
}
for name, places in favorite_places.items():
    print(name.title() + ":")
    for place in places:
        print("\t" + place.title())

print('====')
friends = {
    'ZhangYunyi': [0,2,3,4],
    'LiXia': [12,34,5,6],
    'LiuJun': [2],
    'XuYuchi': [4,22],
    'PanChunyun': [11,23,4,1,3,7,3,35],
}

for name,numbers in friends.items():
    print(name.title() + ":")
    for number in numbers:
        print("\t" + str(number))
        
print("====")
cities = {
    'ShangHai' : {
        'country':'China',
        'population':50000000,
        'fact':'ecnomic city',
    },
    'London' : {
        'country':'England',
        'population':40000000,
        'fact':'soccer city',
    },
    'Bacilona' : {
        'country':'Spain',
        'population':30000000,
        'fact':'trip city',
    },
}

for city,info in cities.items():
    print(city.title() + ":")
    for key, value in info.items():
        if key == 'population':
            print("\t" + key.title() + ":" + str(value))
        else:
            print("\t" + key.title() + ":" + value.title())

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值