字典的嵌套

  • Q:在列表中嵌套字典
  • A:将字典放到列表中
# 下面是将字典放到列表中的例子
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)
------------------------------------------------------------------
{'color': 'green', 'points': 5}
{'color': 'yellow', 'points': 10}
{'color': 'red', 'points': 15}

# 外星人不止三个,且每个外星人都是用代码生成的,我们可以用range()生成

# 创建一个用于存储外星人的空列表
aliens = []

# 创建30个绿色的外星人
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)))
------------------------------------------------------------------
{'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', 'color': 'green', 'points': 5}
...
Total number of aliens: 30

# 修改列表中的字典的例子

# 创建一个用于存储外星人的空列表
alien = []

# 创建30个绿色的外星人
for alien_number in range (0,30):
    new_alien = {"color" : "green","points" : 5,"speed" : "slow"}
    aliens.append(new_alien)

for alien in aliens[0:3]:
    if alien["color"] == "green":
        alien["color"] = "yellow"
        alien["speed"] = "midium"
        alien["point"] = 10 

# 显示前五个外星人
for alien in alien[0:5]:
    print(alien)
print("...")
------------------------------------------------------------------------------------
{'speed': 'medium', 'color': 'yellow', 'points': 10}
{'speed': 'medium', 'color': 'yellow', 'points': 10}
{'speed': 'medium', 'color': 'yellow', 'points': 10}
{'speed': 'slow', 'color': 'green', 'points': 5}
{'speed': 'slow', 'color': 'green', 'points': 5}

  • Q:在字典中嵌套列表
  • A:列表作为键值对的值
# 下面是在字典中嵌套列表的例子
pizza = {
'crust': 'thick',
'toppings': ['mushrooms', 'extra cheese'],
}        
# 概述所点的比萨
print("You ordered a " + pizza['crust'] + "-crust pizza " + "with the following toppings:")
for topping in pizza['toppings']:
    print("	" + topping)
------------------------------------------------------------------
You ordered a thick-crust pizza with the following toppings:
    mushrooms
    extra cheese
# 当字典需要将一个键关联到多个值时,可以在字典中嵌套一个列表

# 下面时另一个例子
favorite_languages = {
    'jen': ['python', 'ruby'],
    'sarah': ['c'],
    'edward': ['ruby', 'go'],
    'phil': ['python', 'haskell'],
}
for name, languages in favorite_languages.items():
    print("
" + name.title() + "'s favorite languages are:")
    for language in languages:
        print("	" + language.title())
------------------------------------------------------------------
Jen's favorite languages are:
    Python
    Ruby

Sarah's favorite languages are:
    C

Phil's favorite languages are:
    Python
    Haskell

Edward's favorite languages are:
    Ruby
    Go

  • Q:在字典中嵌套字典
  • A:字典作为键值对的值
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)
    full_name = user_info['first'] + " " + user_info['last']
    location = user_info['location']

    print("	Full name: " + full_name.title())
    print("	Location: " + location.title())
------------------------------------------------------------------
Username: aeinstein
    Full name: Albert Einstein
    Location: Princeton

Username: mcurie
    Full name: Marie Curie
    Location: Paris
原文地址:https://www.cnblogs.com/goodhelper007/p/dictionary_nested.html