Python: Iterating through a dictionary gives me “int object not iterable”

后端 未结 3 1936
不知归路
不知归路 2020-12-14 14:57

Here\'s my function:

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


        
3条回答
  •  余生分开走
    2020-12-14 15:20

    The for k, v syntax is a short form of the tuple unpacking notation, and could be written as for (k, v). This means that every element of the iterated collection is expected to be a sequence consisting of exactly two elements. But iteration on dictionaries yields only keys, not values.

    The solution is it use either dict.items() or dict.iteritems() (lazy variant), which return sequence of key-value tuples.

提交回复
热议问题