我正在尝试从for循环的打印输出中构建列表。
在下面的示例中,基于一些数据,我对一些计算(所有这些都需要)进行了一个for循环,以创建一个新变量,该变量在每次迭代后打印。我需要从此新变量的打印值创建一个列表。
数据和for循环代码:
from collections import defaultdict
from itertools import permutations
#data
list_type = [['A', 'B'], ['B'], ['A', 'B', 'C', 'D']]
list_xx = [[1, 5], [3], [2, 7, 3, 1]]
#for-loop
wd = defaultdict(float)
for i, x in zip(list_type, list_xx):
# staff 1
if len(i) == 1:
print('aaa')
continue
# staff 2
pairs = list(permutations(i, 2))
dgen = (value[0] - value[1] for value in permutations(x, 2))
# staff 3
for team, result in zip(i, x):
win_comp_past_difs = sum(wd[key] for key in pairs if key[0] == team)
print(win_comp_past_difs)
# staff 4
for pair, diff in zip(pairs, dgen):
wd[pair] += diff
# which prints the following result:
0.0
0.0
aaa
-4.0
4.0
0.0
0.0
我需要从此for循环的打印值构造一个列表,该列表应如下所示:
[0, 0, 'aaa', -4, 4, 0, 0]
我真的很希望在Python中做到这一点的有效方法。