获取函数中元组解包的列表

丹尼尔·罗德里格斯(Daniel Rodriguez)

我有一个给定的函数,需要不同的输入(例如):

def myfunction(x, y, z):
    a = x,y,z
    return a

然后,此for循环:

tripples = [('a', 'b', 'c'), ('d', 'e', 'f'), ('g', 'h', 'i'), ('j', 'k', 'm')]
for tripple in tripples:
    lst.append(myfunction(*tripple))
lst

像这样工作:

[('a', 'b', 'c'), ('d', 'e', 'f'), ('g', 'h', 'i'), ('j', 'k', 'm')]

我想运行它i in range(n)并获得列表列表作为输出,

for i in range(3):
    for tripple in tripples:
        lst_lst.append(myfunction(*tripple))
lst_lst

输出:

[('a', 'b', 'c'),
 ('d', 'e', 'f'),
 ('g', 'h', 'i'),
 ('j', 'k', 'm'),
 ('a', 'b', 'c'),
 ('d', 'e', 'f'),
 ('g', 'h', 'i'),
 ('j', 'k', 'm'),
 ('a', 'b', 'c'),
 ('d', 'e', 'f'),
 ('g', 'h', 'i'),
 ('j', 'k', 'm')]

所需的输出:

[[('a', 'b', 'c'),
 ('d', 'e', 'f'),
 ('g', 'h', 'i'),
 ('j', 'k', 'm')],
 [('a', 'b', 'c'),
 ('d', 'e', 'f'),
 ('g', 'h', 'i'),
 ('j', 'k', 'm')],
 [('a', 'b', 'c'),
 ('d', 'e', 'f'),
 ('g', 'h', 'i'),
 ('j', 'k', 'm')]]

如果有帮助,请使用完整代码:

def myfunction(x, y, z):
    a = x,y,z
    return a

lst = []
lst_lst = []
tripples = [('a', 'b', 'c'), ('d', 'e', 'f'), ('g', 'h', 'i'), ('j', 'k', 'm')]
for tripple in tripples:
    lst.append(myfunction(*tripple))
for i in range(3):
    for tripple in tripples:
        lst_lst.append(myfunction(*tripple))
lst_lst
布莱恩·伍
def myfunction(x, y, z):
    a = x,y,z
    return a

lst = []
tripples = [('a', 'b', 'c'), ('d', 'e', 'f'), ('g', 'h', 'i'), ('j', 'k', 'm')]

for i in range(3):
    lst_lst = []
    for tripple in tripples:
        lst_lst.append(myfunction(*tripple))

    lst.append(lst_lst)

print(lst)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章