我有一个函数:如何从Python中的For循环返回值?
def getvalues():
global avalue
global bvalue
dictt = getresults()
for key , value in dictt.iteritems():
avalue = key
bvalue = dictt[key]
print avalue , bvalue
这将打印安勤的价值,bvalue 输出:
C123 1
C245 2
C456 2
C565 3
C654 1
但是,外面返回值循环不会遍历over.Like此:
def getvalues():
global avalue
global bvalue
dictt = getresults()
for key , value in dictt.iteritems():
avalue = key
bvalue = dictt[key]
return avalue , bvalue
我需要一个在此函数外部使用的avalue,bvalue。如何做到这一点? 如果我使用回报,我得到的输出作为
C654 1
我需要的上述相同的输出与所有安勤,bvalue,我可以在其他功能使用
你可以用'list'追加说''avalue = [],bvalue = []'并使用像'avalue.append(键),bvalue.append(getvalues [键])'返回这些,你可以遍历它们使用外部函数 –
不知道你想要什么,但这可能是[带有yield的生成器](https://wiki.python.org/moin/Generators)的情况(第三个代码示例)。 – Evert
你的代码毫无意义;它会调用自己,直到Python耗尽堆栈,并且它似乎将该函数的名称与字典的名称混淆。请发布实际运行的代码,并按照你所说的做。 – kindall