I want to print 'array' inside the dictionary but my code gives me 'each value' of the array.
for example, "array_ex" is a dictionary and has values like below with 12 rows for each array...
{"0_array": array([[17., 20., 15., ..., 42., 52., 32.],
[24., 33., 19., ..., 100., 120., 90.],
...,
[2., 3., 4., ..., 1., 3., 4.],
[10., 11., 12., ..., 13., 16., 17.]]),
"1_array": array([[20., 20., 15., ..., 42., 43., 35.],
[52., 33., 22., ..., 88., 86., 90.],
...,
[10., 11., 17., ..., 71., 23., 24.],
[34., 44., 28., ..., 42., 43., 17.]])}
and I want to get each row of the array as a result.
array([17., 20., 15., ..., 42., 52., 32.])
However, my code returns each value of the array. How can I fix my code?
for i in array_ex:
for j in range(13): #cuz each key has 12 arrays
for m in array_ex[i][j]:
print(m)
CodePudding user response:
Simply loop over the rows:
for i,a in array_ex.items(): # or for a in array_ex.values()
for row in a:
print(row)
CodePudding user response:
Well, you never create any array so you dont get one. What you will want to do is something like this:
for i in array_ex:
result[]
for j in range(13): #cuz each key has 12 arrays
for m in array_ex[i][j]:
result.append(m)
print(result)
CodePudding user response:
You can use itertools.chain
and list.extend()
like below:
import itertools
res = []
for arr in dct.values():
res.extend(itertools.chain.from_iterable(arr))
print(res)
Output:
[17.0,20.0,15.0,42.0,52.0, ... ]
Explanation:
>>> list(itertools.chain.from_iterable([[], [1, 2], [2]]))
[1, 2, 2]