'''
字典名 = {键1:值,键2:值,...}
'''
'''
访问字典中的值:字典名 = 【键】
'''
alien_0 = {'color':'green','points':5}
print(alien_0['color'])
print(alien_0['points'])
'''
添加键值对
键值对的排列顺序与添加顺序无关。只关心键值对之间的关联关系
'''
print('--------')
alien_0 = {'color':'green','positions':5}
alien_0['x_position'] = 0
alien_0['y_position'] = 25
print(alien_0)
'''
定义一个空字典然后往里面添加值
'''
print('--------')
alien_0 = {}
alien_0['color'] = 'green'
alien_0['positions'] = 5
print(alien_0)
'''
修改字典中的值
'''
print('--------')
alien_0 = {'x_position':0,'y_position':25,'speed':'medium'}
if alien_0['speed'] == 'slow':
x_increment = 1
elif alien_0['speed'] == 'medium':
x_increment = 2
else:
x_increment = 3
alien_0['x_position'] = alien_0['x_position'] + x_increment
print('new x_position is '+str(alien_0['x_position']))
''''
删除键值对
'''
print('--------')
alien_0 = {'x_position':0,'y_position':25,'speed':'medium'}
del alien_0['x_position']#永久删除
print(alien_0)
'''
类似对象组成的字典
'''
print('--------')
favorite_languages = {
'jen':'python',
'sarah':'C',
'edward':'ruby',
}
print('sarahs favorite language is\t'+ favorite_languages['sarah'].title()+ '.') ''' 遍历字典 ''' print('--------') favorite_languages = { 'jen':'python', 'sarah':'C', 'edward':'ruby', } for name,language in favorite_languages.items():#同时申明两个变量,方法.items()表示返回一个键值对列表 print('\nname:'+name) print('language:'+language) ''' 遍历字典中的所有键 ''' print('--------') favorite_languages = { 'jen':'python', 'sarah':'C', 'edward':'ruby', } for name in favorite_languages.keys():#.keys()表示取字典中的键 print(name.title()) print('--------') favorite_languages = { 'jen':'python', 'sarah':'C', 'edward':'ruby', } friends = ['sarah','edward'] for name in favorite_languages:#与上面的功能相同,遍历时默认遍历所有的键 print(name.title()) if name in friends: print(name.title()+ ' I see your favorite language is '+ favorite_languages[name].title()+#不能写成favorite_languages['name'].title()+ '!') #这样写相当于找favorite_languages字典中键叫name的对应值 ''' 顺序遍历所有键:1、for 循环中对返回值的键排序 ''' print('--------') favorite_languages = { 'jen':'python', 'sarah':'C', 'edward':'ruby', } for name in sorted(favorite_languages.keys()): print(name.title()+' thank you for taking the poll.') ''' 遍历列表中的所有值 ''' print('--------') favorite_languages = { 'jen':'python', 'sarah':'C', 'edward':'ruby', } for language in favorite_languages.values(): print(language.title()) ''' 剔除重复项 ''' print('--------') favorite_languages = { 'jen':'python', 'sarah':'C', 'edward':'ruby', 'phil':'C', } for language in set(favorite_languages.values()): print(language.title()) ''' 嵌套:1、将字典嵌套进列表。2、将列表嵌套进字典 ''' print('--------') aliens = [] for alien_number in range(30): new_alien = {'color':'green','point':5,'speed':'slow'} aliens.append(new_alien) for alien in aliens[:5]: print(alien) print('Total number of aliens: '+str(len(aliens)))#len()为求长度 print('--------') aliens = [] for alien_number in range(30):#0~29的数总共三十个 new_alien = {'color':'green','point':5,'speed':'slow'} aliens.append(new_alien) for alien in aliens[:3]: alien['color'] = 'yellow' alien['speed'] = 'medium' alien['point'] = 10 for alien in aliens[:5]: print(alien) print('--------') aliens = [] for alien_number in range(5):#0~29的数总共三十个 new_alien = {'color':'green','point':5,'speed':'slow'} aliens.append(new_alien) for alien in aliens[:3]: alien['color'] = 'yellow' alien['speed'] = 'medium' alien['point'] = 10 for alien in aliens: if alien['color'] == 'yellow': alien['color'] = 'red' alien['speed'] = 'fast' alien['point'] = 15 for alien in aliens[:3]: print(aliens) ''' 字典中嵌套列表 ''' print('--------') 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(language.title())
'''
字典中存储字典
'''
print('--------')
users = {
'aeinstein':{
'first':'albert',
'last':'einstein',
'location':'princeton',
},
'mcurite':{
'first':'marie',
'last':'curie',
'location':'paris',
},
}
for user_name,user_info in users.items():
print('\nUsername: '+user_name)
full_name = user_info['first'].title()+user_info['last'].title()
print('\nFullname is '+full_name)
print('\nLocation is '+user_info['location'])
python从入门到实践第六章——字典
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- # -*- coding = utf - 8 -*- #大家好,今天是2018年12月21日,有点阴天,我是皮卡丘...