'Append for-loop output to a single array
Given that print((sum(int(record[-1]) for record in groupOfRecords))) outputs:
0
0
0
2
10
0
0
0
0
2
10
0
0
0
0
6
0
0
After appending each int output to an empty array groups:
groups.append((sum(int(record[-1]) for record in groupOfRecords)))
print(groups) outputs the following:
[0]
[0, 0]
[0, 0, 0]
[0, 0, 0, 2]
[0, 0, 0, 2, 10]
[0, 0, 0, 2, 10, 0]
[0, 0, 0, 2, 10, 0, 0]
[0, 0, 0, 2, 10, 0, 0, 0]
[0, 0, 0, 2, 10, 0, 0, 0, 0]
[0, 0, 0, 2, 10, 0, 0, 0, 0, 2]
[0, 0, 0, 2, 10, 0, 0, 0, 0, 2, 10]
[0, 0, 0, 2, 10, 0, 0, 0, 0, 2, 10, 0]
[0, 0, 0, 2, 10, 0, 0, 0, 0, 2, 10, 0, 0]
[0, 0, 0, 2, 10, 0, 0, 0, 0, 2, 10, 0, 0, 0]
[0, 0, 0, 2, 10, 0, 0, 0, 0, 2, 10, 0, 0, 0, 0]
[0, 0, 0, 2, 10, 0, 0, 0, 0, 2, 10, 0, 0, 0, 0, 6]
[0, 0, 0, 2, 10, 0, 0, 0, 0, 2, 10, 0, 0, 0, 0, 6, 0]
[0, 0, 0, 2, 10, 0, 0, 0, 0, 2, 10, 0, 0, 0, 0, 6, 0, 0]
Whereas the desired result is simply the last array: [0, 0, 0, 2, 10, 0, 0, 0, 0, 2, 10, 0, 0, 0, 0, 6, 0, 0] in order for print(sum(groups)) to output 30
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
