访问数组的行,在数组的数组内部? [英] Accessing rows of an array, inside an array of arrays?

查看:87
本文介绍了访问数组的行,在数组的数组内部?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有

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 = ...

我要访问的第1行H [0],但接下来要访问H [1]中的行,依此类推。

I want to access row 1 of H[0], but then move onto accessing the rows within H[1] and so on.

我的问题:

1)如何调用H [0]的第1行?

1) How do i call on row 1 of H[0]?

2)然后,我如何遍历H [0]中的所有行,然后循环运行到H [1]?

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)

感谢所有帮助。谢谢。

编辑:我现在知道我可以通过 H [0] [1] 但我实际上需要数组的每一行中的第0列值。因此,我需要 H [0] [1] 中的 11 。我该怎么做?

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?

推荐答案

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

更新:

另一个示例显示了遍历数组,其行和元素的情况:

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))

输出:

array 0
    row 0
    1
    2
    row 1
    3
    4
array 1
    row 0
    5
    6
    row 1
    7
    8

这篇关于访问数组的行,在数组的数组内部?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆