列表中存储字典,字典中存储列表,字典中存储字典--python基础学习

字典有两部分,一个item,一个value

列表中存储字典
创建一个用于存储外星人的空列表
aliens = []
创建30个绿色的外星人
❶ for alien_number in range(30):
❷ new_alien = {‘color’: ‘green’, ‘points’: 5, ‘speed’: ‘slow’}
❸ aliens.append(new_alien)
[ {‘color’: ‘green’, ‘points’: 5, ‘speed’: ‘slow’},
{‘color’: ‘green’, ‘points’: 5, ‘speed’: ‘slow’},
{‘color’: ‘green’, ‘points’: 5, ‘speed’: ‘slow’},
{‘color’: ‘green’, ‘points’: 5, ‘speed’: ‘slow’},
{‘color’: ‘green’, ‘points’: 5, ‘speed’: ‘slow’}]

字典存储列表:
favorite_languages = {
‘jen’: [‘python’, ‘ruby’],
‘sarah’: [‘c’],
‘edward’: [‘ruby’, ‘go’],
‘phil’: [‘python’, ‘haskell’],
}

字典中存储字典:
users = {
‘aeinstein’: {
‘first’: ‘albert’,
‘last’: ‘einstein’,
‘location’: ‘princeton’,
},
‘mcurie’: {
‘first’: ‘marie’,
‘last’: ‘curie’,
‘location’: ‘paris’,
},
}
❶ for username, user_info in users.items():
❷ print("\nUsername: " + username)
❸ full_name = user_info[‘first’] + " " + user_info[‘last’]
location = user_info[‘location’]
❹ print("\tFull name: " + full_name.title())
print("\tLocation: " + location.title())

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值