Python初心者の道--辞書とリストの相互ネストと実現

11933 ワード

'''    '''
                
'''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('
'
,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('
'
,city,"'s population is",character['population'],' located in the',character['location'])