Python:通过字典迭代可以让我“int object not iterable” [英] Python: Iterating through a dictionary gives me "int object not iterable"

查看:2136
本文介绍了Python:通过字典迭代可以让我“int object not iterable”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的功能:

def printSubnetCountList(countList):
    print type(countList)
    for k, v in countList:
        if value:
            print "Subnet %d: %d" % key, value

以下是输入函数时,传递给该字典的函数:

Here's the output when the function is called with the dictionary passed to it:

<type 'dict'>
Traceback (most recent call last):
  File "compareScans.py", line 81, in <module>
    printSubnetCountList(subnetCountOld)
  File "compareScans.py", line 70, in printSubnetCountList
    for k, v in countList:
TypeError: 'int' object is not iterable

任何想法?

推荐答案

尝试这个

for k in countList:
    v= countList[k]

或这个

for k, v in countList.items():

请阅读:http://docs.python.org/library/stdtypes.html#mapping-types-dict

这篇关于Python:通过字典迭代可以让我“int object not iterable”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆