Reputation: 161
Say i have:
H = [array(a), array(b), array(c)...]
a = [[1,2,3,4,5,6],
[11,22,33,44,55,66], #row 1 of H[0]
[111,222,333,444,555,666]]
b = [[7,8,9,0,1,2],
[77,88,99,00,11,22],
[777,888,999,000,111,222]]
c = ...
I want to access row 1 of H[0], but then move onto accessing the rows within H[1] and so on.
My question(s):
1) How do i call on row 1 of H[0]?
2) How do i then, after going through all rows in H[0], make a loop run to H[1]?
I know i need something like
for i in range(len(H)):
do stuff to rows until the last row in H[i]
move onto H[i+1]
do same stuff
stop when (criteria here reached)
All help is appreciated. Thank you.
Edit: I now know I can access this by H[0][1]
but i actually need the 0th column values within each row of the arrays. So, i need 11
from H[0][1]
. How do i do this?
Upvotes: 5
Views: 24841
Reputation: 39546
H = [a, b, c]
Right? If so, then answers:
1)
H[0][1] # 0 - "a" array; 1 - row with index 1 in "a"
2)
for arr in H: # for array in H: a,b,c,...
for row in arr: # for row in current array
# do stuff here
Upd:
One more example shows iterating through arrays, their's rows and elements:
a = [[1, 2],
[3, 4]]
b = [[5, 6],
[7, 8]]
H = [a, b]
for arr_i, arr in enumerate(H):
print('array {}'.format(arr_i))
for row_i, row in enumerate(arr):
print('\trow {}'.format(row_i))
for el in row:
print('\t{}'.format(el))
Output:
array 0
row 0
1
2
row 1
3
4
array 1
row 0
5
6
row 1
7
8
Upvotes: 4
Reputation: 896
What about:
for array in H:
for row in array:
# do stuff
This loop automatically moves on to the next array when the current array is finished.
If you need indices for the arrays then something like:
for array in H:
for i, row in enumerate(array):
for j, value in enumerate(row):
# do stuff
Upvotes: 2
Reputation: 973
It is difficult to understand what you are trying to do.
From what I understand though you are trying to loop through H and then thought the arrays that comprise it.
To do that you can do something like
for array in H:
for sub_array in array:
# do stuff
Otherwise if you want H[1] to access the information in a[1] and for H[3] to access the information in b[1] you can flatten the list. There are two ways to do this
flat_list = sum(H, [])
or
import itertools
for sub_array in itertools.chain(H):
# do stuff
Upvotes: 0