Python基础——#5字典

#5字典

前面介绍了方括号的列表boxs=[];圆括号的元组boxs=();本章将介绍的是花括号的字典boxs={}

​ 在字典中将引入键-值对(key-value)的概念,字典是一系列的键-值对的组合。每个键都与一个值相关联。通过建可以访问其值。与键相关联的可以是数字、字符串、列表甚至是字典。类似于大饼卷万物,任何python对象都能作为字典中的值。

alien_0={'color':'green','point':'5'}
print(alien_0['color'])
green

添加键值对

alien_0={'color':'green','point':'5'}
alien_0['x_position']=0
alien_0['y_position']=25
print(alien_0)
{'color': 'green', 'y_position': 25, 'x_position': 0, 'point': '5'}

和列表一样,有的时候,我们可以只创建一个空字典,然后再用代码添加各个键 值对。

alien_0={}
alien_0['color']='green'
alien_0['points']=5
alien_0['x_position']=0
alien_0['y_position']=25
print(alien_0)
{'color': 'green', 'points': 5, 'x_position': 0, 'y_position': 25}

修改字典中的值

直接对键-值对进行修改

alien_0={'color': 'green', 'points': 5, 'x_position': 0, 'y_position': 25}
alien_0['color']='blue'
print(alien_0)
{'color': 'blue', 'points': 5, 'x_position': 0, 'y_position': 25}

删除键-值对

del

alien_0={'color': 'green', 'points': 5, 'x_position': 0, 'y_position': 25}
del alien_0['color']
print(alien_0)
{'points': 5, 'x_position': 0, 'y_position': 25}

遍历字典

遍历键-值对 .item()
alien_0={'color': 'green', 'points': '5', 'x_position': '0', 'y_position': '25'}
for key, value in alien_0.items():
	print("key:"+key)
	print("value:"+value)

不报错

key:color
value:green
key:points
value:5
key:x_position
value:0
key:y_position
value:25
alien_0={'color': 'green', 'points': 5, 'x_position': 0, 'y_position': 25}
for key, value in alien_0.items():
	print("key:"+key)
	print("value:"+value)

报错(原因在于后面三个键值对的值是整型数字)

key:color
value:green
key:points
Traceback (most recent call last):
  File "D:\PYTHONSPACE\�ֵ�.py", line 30, in <module>
    print("value:"+value)
TypeError: cannot concatenate 'str' and 'int' objects
[Finished in 0.3s with exit code 1]
[shell_cmd: python -u "D:\PYTHONSPACE\字典.py"]
[dir: D:\PYTHONSPACE]

解决办法

alien_0={'color': 'green', 'points': 5, 'x_position': 0, 'y_position': 25}
for key,value in alien_0.items():
	print("key:"+str(key))#直接将所有key值转为str再输出,既不影响源字典的数据类型,也能成功输出
	print("value:"+str(value))
遍历键 .keys()
alien_0={'color': 'green', 'points': 5, 'x_position': 0, 'y_position': 25}
for key in alien_0.keys():
	print(key)
color
points
x_position
y_position

按顺序遍历键

alien_0={ 'points': 5, 'x_position': 0, 'y_position': 25,'color': 'green',}
for key in alien_0.keys():
 	print(key)
for key in sorted(alien_0.keys()):
	print(key)
color
points
x_position
y_position
color
points
x_position
y_position
遍历值 .values()
alien_0={'color': 'green', 'points': 5, 'x_position': 0, 'y_position': 5}
for value in alien_0.values():
	print(value)
green
5
0
5
去除重复值 .set( )
alien_0={'color': 'green', 'points': 5, 'x_position': 0, 'y_position': 5}
for value in set(alien_0.values()):
	print(value)
0
green
5

嵌套

字典和列表可以相互嵌套也可以自己嵌套自己

在列表中存储字典

建立30个外星人字典,并存到外星人列表中

aliens=[]

for alien_number in range(30):
	new_alien={'color': 'green', 'points': 5, 'x_position': 0, 'y_position': 5}
	aliens.append(new_alien)

for alien in aliens[0:5]:
	print(alien)

print(str(len(aliens)))

修改外星人列表中前5个外星人的部分值

aliens=[]

for alien_number in range(30):
	new_alien={'color': 'green', 'points': 5, 'x_position': 0, 'y_position': 5}
	aliens.append(new_alien)


for alien in aliens[:5]:
	if alien['color']=='green':
		alien['color']='yellow'
		alien['points']=8



for alien in aliens[0:10]:
	print(alien)

print(str(len(aliens)))
{'color': 'yellow', 'points': 8, 'x_position': 0, 'y_position': 5}
{'color': 'yellow', 'points': 8, 'x_position': 0, 'y_position': 5}
{'color': 'yellow', 'points': 8, 'x_position': 0, 'y_position': 5}
{'color': 'yellow', 'points': 8, 'x_position': 0, 'y_position': 5}
{'color': 'yellow', 'points': 8, 'x_position': 0, 'y_position': 5}
{'color': 'green', 'points': 5, 'x_position': 0, 'y_position': 5}
{'color': 'green', 'points': 5, 'x_position': 0, 'y_position': 5}
{'color': 'green', 'points': 5, 'x_position': 0, 'y_position': 5}
{'color': 'green', 'points': 5, 'x_position': 0, 'y_position': 5}
{'color': 'green', 'points': 5, 'x_position': 0, 'y_position': 5}
30

还可以用elif继续修改

在字典中存储列表
在字典中存储字典
练习
#1
dog={'type':'land','hostname':'alan'}
cat={'type':'land','hostname':'barry'}
bird={'type':'sky','hostname':'elizabeth'}
pets=[dog,cat,bird]
for pet in pets:
	for key,value in pet.items():
		if key=='type':
			print(value)

land
land
sky
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值