shaoheshaohe 发表于 2019-11-4 22:11:53

Python遍历字典dict的几种方法


[*]#!/usr/bin/python
[*]
[*]dict={"a":"apple","b":"banana","o":"orange"}
[*]
[*]print "##########dict######################"
[*]for i in dict:
[*]      print "dict[%s]=" % i,dict
[*]
[*]print "###########items#####################"
[*]for (k,v) indict.items():
[*]      print "dict[%s]=" % k,v
[*]
[*]print "###########iteritems#################"
[*]for k,v in dict.iteritems():
[*]      print "dict[%s]=" % k,v
[*]
[*]print "###########iterkeys,itervalues#######"
[*]for k,v in zip(dict.iterkeys(),dict.itervalues()):
[*]      print "dict[%s]=" % k,v

页: [1]
查看完整版本: Python遍历字典dict的几种方法