如何返回元组值1,值2..etc 一个接一个
def data ():
List = [(1,"xyz",1.2), (1,"sgz",1.2), ...]
return (1,"xyz",1.2), (1,"sgz",1.2), ...
答案 0 :(得分:1)
使用yield
代替return
,如下所示:
>>> def data():
... for i in [(1,"xyz",1.2), (1,"sgz",1.2)]:
... yield i
...
>>> for x in data():
... print(x)
...
(1, 'xyz', 1.2)
(1, 'sgz', 1.2)
现在data()
就像列表一样 - 每次调用data()
时,都会返回列表中的下一个项目。
答案 1 :(得分:0)
仍然无法弄清楚你为什么要那样做。但是如果你想为列表中的每个元组分配变量,你可以执行以下操作(考虑到你知道列表有多少项):
>>> def data():
List = [(1,"xyz",1.2), (1,"sgz",1.2), ...]
return List
>>> a, b, c = data()
>>> a
(1,"xyz",1.2)
>>> b
(1,"sgz",1.2)