python字典的4种遍历:键,值,项,键值对
遍历字典:键,值,项,键值对
·
一、遍历字典:键,值,项,键值对
dict={"name":"lily","age":18}
#1.遍历字典的键key:
for key in dict.keys():
print(key)
#2.遍历字典的值value:
for value in dict.values():
print(value)
#3.遍历字典的键和值(以元组的格式返回):
for item in dict.items():
print(item)
#4.遍历字典的键和值:
for key,value in dict.items():
print(key,value)
二、字典的其他操作
1、判断一个键是否存在于字典中用“in”或“not in”
if "name" in dict:
print(dict["name"])
2、字典的标准操作:
-, <, >, <=, >=, == ,!=, and ,or ,not
dict={"name":"lily","age":18}
dict0={"name":"lily","age":10}
print(dict==dict0)
3、字典不支持+ , 重复操作*
更多推荐
已为社区贡献2条内容
所有评论(0)