当前位置: 移动技术网 > IT编程>脚本编程>Python > Python中的字典遍历备忘

Python中的字典遍历备忘

2019年03月23日  | 移动技术网IT编程  | 我要评论

虺文忠,江泽慧的丈夫,2013北京公务员

备忘一下python中的字典如何遍历,没有什么太多技术含量.仅供作为初学者的我参考.

复制代码 代码如下:

#!/usr/bin/env python
# coding=utf-8
demodict = {'1':'chrome', '2':'android'}

for key in demodict.keys():
    print key

for value in demodict.values():
    print value

for key in demodict:
    print key, demodict[key]


for key, value in demodict.items():
    print key, value

for key in demodict.iterkeys():
    print key

for value in demodict.itervalues():
    print value

for key, value in demodict.iteritems():
    print key, value

print 'dict.keys()=', demodict.keys(), ';dict.iterkeys()=', demodict.iterkeys()

interitems和iterms区别

参考 http://stackoverflow.com/questions/10458437/python-what-is-the-difference-between-dict-items-and-dict-iteritems

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网