Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing rows of an array, inside an array of arrays?

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?

like image 978
layces Avatar asked Mar 16 '16 17:03

layces


2 Answers

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
like image 184
Mikhail Gerasimov Avatar answered Sep 27 '22 00:09

Mikhail Gerasimov


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
like image 26
K. Menyah Avatar answered Sep 24 '22 00:09

K. Menyah