I am trying to construct a list from the printed output of a for-loop.
In the example below based on some data I do a for-loop for some calculations (all of them needed) to create a new variable, which is printed after each iteration. What I need is to create a list from the printed values of this new variable.
The data and the for-loop code:
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
What I need is to construct a list from the printed values of this for-loop, which should look like this:
[0, 0, 'aaa', -4, 4, 0, 0]
I would really value an efficient way of doing this in Python.