6、python的字典
python 字典
表现形式:键值对
定义符号:{}
键在一个字典中必须唯一,不能重复;若重复覆盖(修改)原来的值
字典中键,理论上来说,只要不唯一,可以随便搞,但是为了头发着想,还是悠着点!!! /(ㄒoㄒ)/~~
字典的基操
访问
a = {0:"apple" , 1:"huawei" , 2:"xiaomi"}
print(a[0]) # apple
添加
a = {0:"apple" , 1:"huawei" , 2:"xiaomi"}
a[3] = "祖安小王子的坟头蹦迪入场券*1"
print(a)
空字典创建
a = { }
修改
a = {0:"apple" , 1:"huawei" , 2:"xiaomi"}
a[0] = "泉水周围的地板,被你擦的很亮啊!(╬▔皿▔)凸"
print(a)
删除
a = {0:"apple" , 1:"huawei" , 2:"xiaomi"}
del a[0]
print(a)
遍历字典
a = {0:"apple" , 1:"huawei" , 2:"xiaomi"}
for key ,value in a.items() :
print(key)
print(value)
print("-"*10)
遍历字典2 遍历所有键
a = {0:"apple" , 1:"huawei" , 2:"xiaomi"}
for key in a.keys() :
print(key)
print("-"*10)
遍历字典3 遍历所有值
a = {0:"apple" , 1:"huawei" , 2:"xiaomi"}
for value in a.values() :
print(value)
print("-"*10)