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