多维数组索引和列访问 [英] Multidimension array indexing and column-accessing

查看:144
本文介绍了多维数组索引和列访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像

[[[   1    4    4 ...,  952    0    0]
  [   2    4    4 ...,   33    0    0]
  [   3    4    4 ..., 1945    0    0]
  ..., 
  [4079    1    1 ...,    0    0    0]
  [4080    2    2 ...,    0    0    0]
  [4081    1    1 ...,    0    0    0]]

 [[   1    4    4 ...,  952    0    0]
  [   2    4    4 ...,   33    0    0]
  [   3    4    4 ..., 1945    0    0]
  ..., 
  [4079    1    1 ...,    0    0    0]
  [4080    2    2 ...,    0    0    0]
  [4081    1    1 ...,    0    0    0]]

  .....

 [[   1    4    4 ...,  952    0    0]
  [   2    4    4 ...,   33    0    0]
  [   3    4    4 ..., 1945    0    0]
  ..., 
  [4079    1    1 ...,    0    0    0]
  [4080    2    2 ...,    0    0    0]
  [4081    1    1 ...,    0    0    0]]]

该阵列共有5的数据块。每个数据块具有4081行和第9列。

This array has total 5 data blocks. Each data blocks have 4081 lines and 9 columns.

在这里我的问题是有关访问列,在数据块明智的。结果
我希望索引数据块,行和列,并获得列,并做一些与工作如果循环。我知道如何访问到列二维数组,如:

My question here is about accessing to column, in data-block-wise.
I hope to index data-blocks, lines, and columns, and access to the columns, and do some works with if loops. I know how to access to columns in 2D array, like:

COLUMN_1 =行[0] inputfile中排]

column_1 = [row[0] for row in inputfile]

但我怎么能访问每个每个数据块列?

but how can I access to columns per each data block?

我试过像(inputfile中= 3D以上阵列)

I tried like ( inputfile = 3d array above )

for i in range(len(inputfile)):
    AAA[i] = [row[0] for row in inputfile]
    print AAA[2]

但它说,'名称'AAA'没有定义。如何访问到列,每个数据块?我是否需要做[无]数组?有没有不使用空数组任何其他方式?

But it says 'name 'AAA' is not defined. How can I access to the column, for each data blocks? Should I need to make [None] arrays? Are there any other way without using empty arrays?

另外,我怎么能访问到访问栏的具体内容?像AAA [I] [j]的=第i个数据块,和第一列的第j行。我会用一个更for循环线明智访问?

Also, how can I access to the specific elements of the accessed columns? Like AAA[i][j] = i-th datablock, and j-th line of first column. Shall I use one more for loop for line-wise accessing?

PS)我试图分析在某种​​程度上像

ps) I tried to analyze this 3d array in a way like

for i in range(len(inputfile)):      ### number of datablock = 5
    for j in range(len(inputfile[i])):  ### number of lines per a datablock = 4081
        AAA = inputfile[i][j]        ### Store first column for each datablocks to AAA
        print AAA[0]                 ### Working as I intended to access 1st column. 
        print AAA[0][1]              ### Not working, invalid index to scalar variable. I can't access to the each elemnt. 

不过这样一来,我不能进入到第一列的每一个元素,AAA [0]。我怎样才能获得在这里的每个元素?

But this way, I cannot access to the each elements of 1st column, AAA[0]. How can I access to the each elements in here?

我想,也许2指标还不够,所以我用3 for循环为:

I thought maybe 2 indexes were not enough, so I used 3 for-loops as:

for i in range(len(inputfile)):                ### number of datablock = 5
    for j in range(len(inputfile[i])):         ### number of lines per a datablock = 4081
        for k in range(len(inputfile[i][j])):  ### number of columns per line = 9
           AAA = inputfile[i][j][0]
           print AAA[0]

不过,我无法访问到第一列的每一个元素,它说无效的指数标量。此外,AAA包含每个元素九,就像

Still, I cannot access to the each elements of 1st column, it says 'invalid index to scalar variable'. Also, AAA contains nine of each elements, just like

>>> print AAA
1
1
1
1
1
1
1
1
1
2
2
...
4080
4080
4080
4081
4081
4081
4081
4081
4081
4081
4081
4081

与此类似,每个元素重复9次,这不是我想要的。

Like this, each elements repeats 9 times, which is not what I want.

我希望我的分析过程中使用指标,在分析过程中会使用索引元素。我想访问的列,并获得所有指数的各要素,在这个三维数组。我怎样才能做到这一点?

I hope to use indices during my analysis, will use index as element during analysis. I want to access to the columns, and access to the each elements with all indices, in this 3d array. How can I do this?

推荐答案

除非你使用类似numpy的,Python没有多维数组这样。相反,你所示的结构是一个整数列表的列表清单。 (你的选择的inputfile中作为变量名是混乱这里,这样的变量通常会包含一个文件句柄,遍历​​这将产生每行一个字符串,但我离题。 ..)

Unless you're using something like NumPy, Python doesn't have multi-dimensional arrays as such. Instead, the structure you've shown is a list of lists of lists of integers. (Your choice of inputfile as the variable name is confusing here; such a variable would usually contain a file handle, iterating over which would yield one string per line, but I digress...)

不幸的是,我无法理解你想要什么来完成,但在一个点上,你似乎要由每行​​的第一列的一个列表。这就是这么简单:

Unfortunately, I'm having trouble understanding exactly what you're trying to accomplish, but at one point, you seem to want a single list consisting of the first column of each row. That's as simple as:

column = [row[0] for block in inputfile for row in block]

诚然

,这是不是真的在数学意义上的一列,但它有可能会成为也许你想要的东西。

Granted, this isn't really a column in the mathematical sense, but it might possibly perhaps be what you want.

现在,至于为什么你的其他努力都失败了:

Now, as to why your other attempts failed:

for i in range(len(inputfile)):
    AAA[i] = [row[0] for row in inputfile]
    print AAA[2]

由于错误讯息: AAA 没有定义。 Python没有让你指定给一个未定义的变量的指标,因为它不知道变量是否应该是一个列表,一个字典,或者更多的东西异国情调。特别是对于列表,它也不会让你分配给不存在的索引;相反,追加延长将用于该方法:

As the error message states, AAA is not defined. Python doesn't let you assign to an index of an undefined variable, because it doesn't know whether that variable is supposed to be a list, a dict, or something more exotic. For lists in particular, it also doesn't let you assign to an index that doesn't yet exist; instead, the append or extend methods are used for that:

AAA = []
for i, block in enumerate(inputfile):
    for j, row in enumerate(block):
        AAA.append(row[0])
print AAA[2]

(然而,这是不是很列表COM prehension高效以上。)

(However, that isn't quite as efficient as the list comprehension above.)

for i in range(len(inputfile)):    ### number of datablock = 5
    for j in range(len(inputfile)):     ### number of lines per a datablock = 4081
        AAA = inputfile[i][j]          ### Store first column for each datablocks to AAA
        print AAA[0]      ### Working as I intended to access 1st column. 
        print AAA[0][1]   ### Not working, invalid index to scalar variable. I can't access to the each elemnt. 

有与第二行的范围,并在仰视inputfile中[I]多次的低效一个明显的问题,但真正的问题是在最后一行。此时,AAA指块中的一个的行中的一个;例如,在第一时间通过,鉴于上述数据集,

There's an obvious problem with the range in the second line, and an inefficiency in looking up inputfile[i] multiple times, but the real problem is in the last line. At this point, AAA refers to one of the rows of one of the blocks; for example, on the first time through, given your dataset above,

AAA == [   1    4    4 ...,  952    0    0]

这是一个列表,具有数据结构作为一个整体没有引用。 AAA [0] 工作访问的第一列数, 1 ,因为这是如何操作的列表。该行的第二列将在 AAA [1] ,等等。但 AAA [0] [1] 抛出一个错误,因为它等同于(AAA [0])[1] ,它在此情况下等于(1)[1] ,但号码不能被索引的。 (什么是数字1的第二个元素?)

It's a single list, with no references to the data structure as a whole. AAA[0] works to access the number in the first column, 1, because that's how lists operate. The second column of that row will be in AAA[1], and so on. But AAA[0][1] throws an error, because it's equivalent to (AAA[0])[1], which in this case is equal to (1)[1], but numbers can't be indexed. (What's the second element of the number 1?)

for i in range(len(inputfile)):    ### number of datablock = 5
    for j in range(len(inputfile[i])):     ### number of lines per a datablock = 4081
        for k in range(len(inputfile[i][j])):      ### number of columns per line = 9
           AAA = inputfile[i][j][0]
           print AAA[0]

这时候,你的循环,虽然还是低效的,至少是正确的,如果你想在整个数据结构中的每个数字遍历。在底部,你会发现, inputfile中[I] [J] [K] 是整数 K 在J座 I 的数据结构。但是,你扔出去 K 完全,并打印该行的第一个元素,一旦该行中的每个项目。 (它的确切重复多次,你有列应该是一个线索的事实。)再一次,你不能索引任何进一步一旦你的整数;没有 inputfile中[I] [J] [0] [0]

This time, your for loops, though still inefficient, are at least correct, if you want to iterate over every number in the whole data structure. At the bottom, you'll find that inputfile[i][j][k] is integer k in row j in block i of the data structure. However, you're throwing out k entirely, and printing the first element of the row, once for each item in the row. (The fact that it's repeated exactly as many times as you have columns should have been a clue.) And once again, you can't index any further once you get to the integers; there is no inputfile[i][j][0][0].

当然,一旦你到某个元素,你可以看看,通过改变索引附近的元素。例如,一个三维细胞自动机可能想看看它的每一个邻国。随着数据的边缘适当的修正,并检查,确保每块和行是正确的长度(Python将不会为你做),这可能会是这样:

Granted, once you get to an element, you can look at nearby elements by changing the indexes. For example, a three-dimensional cellular automaton might want to look at each of its neighbors. With proper corrections for the edges of the data, and checks to ensure that each block and row are the right length (Python won't do that for you), that might look something like:

for i, block in enumerate(inputfile):
    for j, row in enumerate(block):
        for k, num in enumerate(row):
            neighbors = sum(
                inputfile[i][j][k-1],
                inputfile[i][j][k+1],
                inputfile[i][j-1][k],
                inputfile[i][j+1][k],
                inputfile[i-1][j][k],
                inputfile[i+1][j][k],
            )
            alive = 3 <= neigbors <= 4

这篇关于多维数组索引和列访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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