简体   繁体   English

多维数组索引和列访问

[英]Multidimension array indexing and column-accessing

I have a 3 dimensional array like 我有一个3维数组

[[[   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]]]

This array has total 5 data blocks. 该阵列总共有5个数据块。 Each data blocks have 4081 lines and 9 columns. 每个数据块具有4081行和9列。

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. 我希望索引数据块,行和列,并访问这些列,并对if循环做一些工作。 I know how to access to columns in 2D array, like: 我知道如何访问2D数组中的列,例如:

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

but how can I access to columns per each data block? 但是如何访问每个数据块的列?

I tried like ( inputfile = 3d array above ) 我尝试过(inputfile = 3d以上的数组)

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

But it says 'name 'AAA' is not defined. 但是它说'name'AAA'没有定义。 How can I access to the column, for each data blocks? 如何访问每个数据块的列? Should I need to make [None] arrays? 我是否需要制作[None]数组? Are there any other way without using empty arrays? 还有其他不使用空数组的方法吗?

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. 像AAA [i] [j] =第i个数据块,第一列的第j行。 Shall I use one more for loop for line-wise accessing? 我应该再使用一个for循环进行逐行访问吗?

ps) I tried to analyze this 3d array in a way like ps)我试图以这种方式分析3d阵列

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. 

But this way, I cannot access to the each elements of 1st column, AAA[0]. 但是通过这种方式,我无法访问第一列AAA [0]的每个元素。 How can I access to the each elements in here? 我如何在这里访问每个元素?

I thought maybe 2 indexes were not enough, so I used 3 for-loops as: 我认为也许2个索引是不够的,所以我使用了3个for循环:

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]

Still, I cannot access to the each elements of 1st column, it says ' invalid index to scalar variable '. 我仍然无法访问第一列的每个元素,它说“ invalid index to scalar variableinvalid index to scalar variable ”。 Also, AAA contains nine of each elements, just like 同样,AAA包含每个元素中的九个,就像

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

Like this, each elements repeats 9 times, which is not what I want. 这样,每个元素重复9次,这不是我想要的。

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. 我想访问此3d数组中的列,并访问具有所有索引的每个元素。 How can I do this? 我怎样才能做到这一点?

A good practice in to leverage zip : 充分利用zip的一个好习惯:

For example: 例如:

>>> a = [1,2,3]
>>> b = [4,5,6]
>>> for i in a:
...  for j in b:
...   print i, b
... 
1 [4, 5, 6]
1 [4, 5, 6]
1 [4, 5, 6]
2 [4, 5, 6]
2 [4, 5, 6]
2 [4, 5, 6]
3 [4, 5, 6]
3 [4, 5, 6]
3 [4, 5, 6]
>>> for i,j in zip(a,b):
...  print i,j
... 
1 4
2 5
3 6

Unless you're using something like NumPy, Python doesn't have multi-dimensional arrays as such. 除非您使用的是NumPy之类的东西,否则Python就没有这样的多维数组。 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...) (您在选择inputfile作为变量名时感到困惑;这样的变量通常包含一个文件句柄,对其进行迭代将每行产生一个字符串,但我离题了。

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]

As the error message states, AAA is not defined. 如错误消息所述,未定义AAA 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. Python不允许您分配未定义变量的索引,因为它不知道该变量是应该是列表,字典还是更奇特的东西。 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: 而是使用appendextend方法:

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

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

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. 第二行的范围存在明显的问题,并且多次查找inputfile [i]效率低下,但真正的问题出在最后一行。 At this point, AAA refers to one of the rows of one of the blocks; 在这一点上,AAA指的是一个块中的一行。 for example, on the first time through, given your dataset above, 例如,第一次给定上面的数据集,

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

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. AAA[0]用于访问第一列1 ,因为这就是列表的操作方式。 The second column of that row will be in AAA[1] , and so on. 该行的第二列将在AAA[1] ,依此类推。 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 . 但是AAA[0][1]会引发错误,因为它等效于(AAA[0])[1] ,在这种情况下,它等于(1)[1] ,但是数字无法索引 (What's the second element of the number 1?) (数字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]

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. 这次,如果您要遍历整个数据结构中的每个数字,则for循环尽管仍然效率不高,但至少是正确的。 At the bottom, you'll find that inputfile[i][j][k] is integer k in row j in block i of the data structure. 在底部,您会发现inputfile[i][j][k]是数据结构第i行第j行的整数k However, you're throwing out k entirely, and printing the first element of the row, once for each item in the row. 但是,您将完全丢掉k ,并为该行中的每个项目打印该行的第一个元素。 (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] . 没有输入文件inputfile[i][j][0][0]

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: 对数据的边缘进行适当的校正,并检查以确保每个块和行的长度正确(Python不会为您做到这一点),这可能类似于:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM