获取在函数中解包元组的列表
Get a list with tuple unpacking in a function
我有一个接受不同输入的给定函数(示例):
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)
你需要使用一个临时列表,它保存一个循环的结果,然后将这些结果添加到最终列表中,并在下一个循环中初始化自身,然后再次保存下一个三元组的结果
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):
tmp =[]
for tripple in tripples:
tmp.append(myfunction(*tripple))
lst_lst.append(tmp)
我有一个接受不同输入的给定函数(示例):
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)
你需要使用一个临时列表,它保存一个循环的结果,然后将这些结果添加到最终列表中,并在下一个循环中初始化自身,然后再次保存下一个三元组的结果
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):
tmp =[]
for tripple in tripples:
tmp.append(myfunction(*tripple))
lst_lst.append(tmp)