Python初学者之路--字典与列表的互相嵌套以及实现

''' 嵌套 '''
                
'''list 嵌套 dictionary'''
alien_0={'color':'green','speed':10,'IQ':0}
alien_1={'color':'green','speed':10,'IQ':0}
alien_3={'color':'green','speed':10,'IQ':0}

aliens=[alien_0,alien_1,alien_3]

for alien in aliens:
    print(alien)

'''if we want more aliens'''

aliens_1=[]
for alien_number in range(20):
    new_alien={'color':'green','speed':10,'IQ':0}
    aliens_1.append(new_alien)

print('Total number of aliens is '+str(len(aliens_1)))

for alien in aliens_1[:2]:
    if alien['color']=='green':
        alien['color']='yellow'
    if alien['speed']==10:
        alien['speed']=5

'''dictionary 嵌套 list'''
#当一个键要关联多个值的时候建议使用
fav_lag={
        'James':['C','python','Matlab'],
        'lucy':['Lingo','c++'],
        'oliVer quEEn':['Chinese']}
for names,lag in fav_lag.items():
    print('\n',names.title(),"'s favourite languages are",end='\t')
    for languages in lag:
        print('\t',languages.lower(),end=' ')

'''dictionary互相嵌套'''

cities={
        'Tj':{'population':3000,'location':'northeeast','Square':100},
        'XiAn':{'population':5000,'location':'northwest','Square':500},
        'ChongQing':{'population':7000,'location':'southwest','Square':600}
        }
for city,character in cities.items():
    print('\n',city,"'s population is",character['population'],' located in the',character['location'])

你可能感兴趣的:(Python初学者之路--字典与列表的互相嵌套以及实现)