欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

python学习笔记——嵌套

程序员文章站 2022-03-27 09:37:29
...

列表里面嵌字典 (存储一个种类的不同信息)

alien_0={'color':'green','point':5}
alien_1={'color':'yellow','point':10}
alien_2={'color':'red','point':15}

aliens=[alien_0,alien_1,alien_2]

for alien in aliens:
    print(alien)
#创建一个空列表
print("...")
for alien_number in range(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']='medium'
        alien['points']=10
    elif alien['color']=='yellow':
        alien['color']='red'
        alien['speed']='fast'
        alien['points']=15
        
for alien in aliens[:5]:
    print(alien)
print("...")

print("Tatal number of aliens:"+str(len(aliens)))#查看创建了多少个外星人

python学习笔记——嵌套

字典嵌列表(用于字典的里键对应多个值(1对n))

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())

python学习笔记——嵌套

 

 

 

 

 

 

 

 字典嵌字典

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())