5

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?

layces
  • 161
  • 2
  • 2
  • 12

3 Answers3

3
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
Mikhail Gerasimov
  • 36,989
  • 16
  • 116
  • 159
  • For that second part, how would i separate my actions/what i'm doing between those two? So if i had `H[i][j]` and `for arr in H:` acted on `i` but `for row in arr:` acted on `j` , i'm confused on where exactly i would place my `i+=1` or `j+=1` @germn – layces Mar 16 '16 at 18:14
  • @TC, in most cases you don't need i, j. Python is not C: in python you work with objects in list, not with it's indexes. In case you really need index of element, you can use enumerate(), see: http://stackoverflow.com/a/522578/1113207 – Mikhail Gerasimov Mar 16 '16 at 19:18
  • Okay so i had improperly named my own things, so at first i wasn't getting a proper recreation, but now i am! My very last question is still: how do i specifically only call on the very first column values for each row? Ex: `1`,`3`,`5`,`7` in your output example? I do not know what they are /called/, using `el`, `row_i` or `row`, and `arr_i` or `arr` ? – layces Mar 16 '16 at 21:11
  • I just ended up erasing all the other columns and backing it up into a separate list of lists. Thank you though, this as a whole answered my questions best. – layces Mar 16 '16 at 22:45
2

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
ml-moron
  • 888
  • 1
  • 11
  • 22
  • Huh, so where would i put my break in this? Would it be at the end of the first for-loop? Or would it be better to put all of this in a `while:` ? – layces Mar 16 '16 at 18:19
  • where do you want to put a break? you didn't mention it in your question. – ml-moron Mar 16 '16 at 18:25
  • I need it to stop at the point i labeled `stop when (criteria here reached)`. And thank you so much for your added part to the answer, can i use that to access only a certain column value inside of `H[i][j]`? So, in my example, if i only needed `11` from `H[0][1]`? – layces Mar 16 '16 at 18:58
  • yeah, sure, `if i == 0 and j == 1: # do stuff` – ml-moron Mar 16 '16 at 19:10
  • Not sure what your break criteria is, but you can put an `if...break` statement in the inner loop if you're ok with it stoppint in the middle of a row. – ml-moron Mar 16 '16 at 19:12
0

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
Jules
  • 973
  • 4
  • 10