#字典:dict 无序,不能使用切片
#列表不能作为k,元组可以作为k
info={"k1":"a","k2":"b","k3":[1,2,"abc"]} #存放键值对
info1={"k1":"a","k2":"b","k3":[1,2,"abc"],"k4":(1,2),"k5",[[1,2],{"k1":"1","kk2","2"}]}
v=info['k1']
print(v) #打印k1
del info['k1'] #删除k1
法1: 遍历字典所有key
for item in info :
print(item)
法2: 遍历字典所有key
for item in info.keys() :
print(item)
for item in info.values() : 遍历字典所有values
print(item)
方法1: 遍历字典所有key,value
for item in info.keys() : 遍历字典所有key,value
print(item,info[item])
方法2:
for k,v in info.items():
print(k,v)
v=dict.fromkeys([“k1”,123,”999”],123) #把[“k1”,123,”999”]当成键,生成字典
info={"k1":"a","k2":"b
python字典
最新推荐文章于 2024-04-20 12:21:36 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)