hal*_*d01 1 python zip append temp
我有一个问题,我还没有找到一个好的解决方案.我正在寻找一种更好的方法来将函数输出附加到两个或更多列表,而不使用临时变量.示例如下:
def f():
return 5,6
a,b = [], []
for i in range(10):
tmp_a, tmp_b = f()
a.append(tmp_a)
b.append(temp_b)
Run Code Online (Sandbox Code Playgroud)
我试过玩像zip(*f())这样的东西,但还没有找到解决办法.任何方式去除那些临时变量将是非常有用的,谢谢!
编辑以获取其他信息:在这种情况下,函数的输出数量始终等于要追加的列表数量.我想要摆脱temps的主要原因是可能有8-10个函数输出的情况,并且有许多临时变量会变得混乱(尽管我真的不喜欢有两个).
第一个解决方案:我们列出所有结果,然后将其转置
def f(i):
return i, 2*i
# First make a list of all your results
l = [f(i) for i in range(5)]
# [(0, 0), (1, 2), (2, 4), (3, 6), (4, 8)]
# then transpose it using zip
a, b = zip(*l)
print(list(a))
print(list(b))
# [0, 1, 2, 3, 4]
# [0, 2, 4, 6, 8]
Run Code Online (Sandbox Code Playgroud)
或者,全部在一行中:
a, b = zip(*[f(i) for i in range(5)])
Run Code Online (Sandbox Code Playgroud)
一个不同的解决方案,在每次迭代时构建列表,以便您可以在构建它们时使用它们:
def f(i):
return 2*i, i**2, i**3
doubles = []
squares = []
cubes = []
results = [doubles, squares, cubes]
for i in range(1, 4):
list(map(lambda res, val: res.append(val), results, f(i)))
print(results)
# [[2], [1], [1]]
# [[2, 4], [1, 4], [1, 8]]
# [[2, 4, 6], [1, 4, 9], [1, 8, 27]]
print(cubes)
# [1, 8, 27]
Run Code Online (Sandbox Code Playgroud)
注意list(map(...)):在 Python3 中,map返回一个生成器,所以如果我们想要执行 lambda,我们必须使用它。list可以。
def f():
return 5,6
a,b = zip(*[f() for i in range(10)])
# this will create two tuples of elements 5 and 6 you can change
# them to list by type casting it like list(a), list(b)
Run Code Online (Sandbox Code Playgroud)