Python中的字典遍历备忘

yipeiwu_com5年前Python基础

备忘一下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

相关文章

Python列表删除元素del、pop()和remove()的区别小结

前言 在python列表的元素删除操作中, del, pop(), remove()很容易混淆, 下面对三个语句/方法作出解释 del语句 del语句可以删除任何位置处的列表元素, 若...

基于Django filter中用contains和icontains的区别(详解)

qs.filter(name__contains="e") qs.filter(name__icontains="e") 对应sql 'contains': 'LIKE BI...

python中pandas.DataFrame排除特定行方法示例

前言 大家在使用Python进行数据分析时,经常要使用到的一个数据结构就是pandas的DataFrame,关于python中pandas.DataFrame的基本操作,大家可以查看这篇...

Python3.6连接Oracle数据库的方法详解

本文实例讲述了Python3.6连接Oracle数据库的方法。分享给大家供大家参考,具体如下: 下载cx_Oracle模块模块: https://pypi.python.org/pypi...

Python cookbook(数据结构与算法)让字典保持有序的方法

本文实例讲述了Python让字典保持有序的方法。分享给大家供大家参考,具体如下: 问题:创建一个字典,同时对字典做迭代或序列化操作时,也能控制其中元素的顺序; 解决方案:可以使用coll...