python使用items()遍历键值对
python使用items()遍历键值对
字典可以用来存储各种方式的信息,所以有很多方式可以通过字典的所有键值对、键或值。
说明
1、即使通过字典,键值对的返回顺序也与存储顺序不同。
2、Python不关心键-值对的存储顺序,只关注键与值之间的关系。
实例
通过fork、vincat.items(),所有键值都是对的,k代表键,v代表值。
>>>cat
{'age':6,'color':'white','city':'beijing'}
>>>fork,vincat.items():
...print(k+'-'+str(v))
...
age-6
color-white
city-beijing
以上就是python使用items()遍历键值对的方法,希望对大家有所帮助。更多Python学习教程请关注IT培训机构:千锋教育。

相关推荐HOT
更多>>
python包的导入方式有几种
python包的导入方式有几种本文教程操作环境:windows7系统、Python3.9.1,DELLG3电脑。1、from...import导入frompackageimportmodule1,mo详情>>
2023-11-08 23:58:44
python参数是什么
python参数是什么1、概念Python中函数的参数是不变的对象。Python函数具有灵活的参数形式,但默认参数必须指向不变的对象。如果默认参数是可变...详情>>
2023-11-08 19:48:30
python使用items()遍历键值对
python使用items()遍历键值对字典可以用来存储各种方式的信息,所以有很多方式可以通过字典的所有键值对、键或值。说明1、即使通过字典,键值对...详情>>
2023-11-08 19:23:48
python按行读取文件的方法比较
python按行读取文件的方法比较1、read方法默认会把文件的所有内容一次性读取到内存。如果文件太大,对内存的占用会非常严重2、readline方法,re...详情>>
2023-11-08 18:22:27