1.概念
在Python中,字典是一系列的键-值对,每个键都与一个值相关联。可以使用键来访问与之相关的值,与键相关的值可以是数字、字符串、列表或者字典。(任何Python中的对象都可以用作字典中的值)
Python只关心字典中的键—值对的关联关系,并不关系键—值对的存储顺序
在Python中,字典用放着一对花括号 { } 中的一系列键-值对表示,键-值之间用冒号“:”分隔,键-值对之间用逗号“,”分隔。例如:
#一个简单的字典示例:
test={'coloe':'yellow','scor':5}
2.访问、添加、修改 字典中的值
#访问字典中的值。要获取与键相关的值,可依次指定字典名和放在方括号内的键,例如:
test={'coloe':'yellow','scor':5}
print(test['color']) #访问test中键为'color'的值
#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
yellow
#添加字典中的值------------------------分割线---------------------------------------------
test={'coloe':'yellow','scor':5} #创建字典并设置两个键-值对
print(test) #输出字典
print("颜色为:"+test['coloe']+"\n分数为:"+test['scor'].__str__())
#使用键访问键对应的值
test['x_loc']=25 #修改/添加键-值对,如果test中有键为'x_loc'的键-值对,则将键为'x_loc'的值修改为25
#如果test中没有键为'x_loc'的键-值对,则将'x_loc':25这个键-值对添加到字典test中
test['y_loc']=10 #同上,,,所以,一个字典中键是唯一的
print(test)
#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
{'coloe': 'yellow', 'scor': 5} #输出字典
颜色为:yellow #输出用键访问对应的值
分数为:5
{'coloe': 'yellow', 'scor': 5, 'x_loc': 25, 'y_loc': 10} #输出执行添加/修改后的字典
#修改字典中的值------------------------分割线---------------------------------------------
test={'coloe':'yellow','scor':5}
print("原来的颜色值:"+test['color'])
test['color']='green' #与添加操作如出一辙
print("修改后的颜色值:"test['color'])
#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
原来的颜色值:yellow
修改后的颜色值:green
3.删除键-值对
test={'coloe':'yellow','scor':5}
print(test)
del test['color'] #使用del语句将相应的键-值对彻底删除,删除的键-值对永远消失了
print(test)
#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
{'coloe':'yellow','scor':5}
{'scor':5}
4.遍历字典
遍历字典、遍历键、遍历值
#可以将一个较大,或者名称相似的字典放在多行之中(意思就是字典可以分行存储,但空格要对的上,如下)
test={'a':1,
'b':2,
'c':3,
}
for key in test: #==for key in test.keys(): 遍历字典时会默认遍历所有键
print("fist print: key="+key)
for key in test.items(): #遍历字典,然后使用items()方法返回一个键-值对
print("\ntwice print:key="+str(key))
for key, value in test.items():
print("\nthree print:key=" + key)
print("value:" + str(value))
for value int test.items() #遍历值
print("遍历值:"+str(value))
#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
fist print: key=a #for key in test 就相当于for key in test.keys():
fist print: key=b #直接遍历字典中的所有键
fist print: key=c
-------------------------------
twice print:key=('a', 1)
#遍历字典,然后使用items()方法返回一个键-值对
twice print:key=('b', 2) #它会根据遍历的键将键与其对应的值都返回
twice print:key=('c', 3)
-------------------------------
three print:key=a
value:1 #遍历字典中的键-值对
three print:key=b
value:2
three print:key=c
value:3
-------------------------------
遍历值:1
遍历值:2
遍历值:3
按顺序遍历键、遍历值时去掉重复的值
#给出一份调查名单,值为1则表示已经参加,为0则表示未参加调查
listone={'李天赐':1,'李云':0,'吕学学':0,'姚慧琪':1}
for name,value in listone.items(): #遍历字典中的键-值对
if value==1:
print(name+"感谢您对我的支持!")
elif value==0:
print(name+"诚心邀请您来参加问卷!!!")
print(set(listone.values())) #set() 去掉重复,提取list.values()中不同的值
#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
李天赐感谢您对我的支持!
李云诚心邀请您来参加问卷!!!
吕学学诚心邀请您来参加问卷!!!
姚慧琪感谢您对我的支持!
{0, 1} #set() 去掉重复后只剩下{0, 1} 两个值
5.嵌套
在字典中存储列表、在列表中存储字典,在字典中存储字典
#在字典中存储列表
apple={
'color':['red','green'],
'shuliang':5}
print(apple['color'])
#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
['red', 'green']
------------------------------------------------------
#在列表中存储字典
apple={'color':'red','shuliang':3}
orange={'color':'orange','shuliang':4}
peach={'color':'red','shuliang':4} #先创建3种水果的字典
flutes=[apple,orange,peach] #水果列表存储3种水果字典
for fulte in flutes:
print(flute) #输出水果列表
#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
{'color': 'red', 'shuliang': 3}
{'color': 'orange', 'shuliang': 4}
{'color': 'red', 'shuliang': 4}
------------------------------------------------------
#在字典中存储字典
users={'小福':{'姓名':'小管','年龄':27,'备注':'尊贵VIP8'},
'小琼':{'姓名':'小明','年龄':18,'备注':'白嫖路人甲'}}
for user,message in users.items():
print(message['备注']+user+",欢迎您的登录!")
#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
尊贵VIP8小福,欢迎您的登录!
白嫖路人甲小琼,欢迎您的登录!