Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 循环提取数组数组中的每个数组_Python_Arrays_Python 3.x_Numpy - Fatal编程技术网

Python 循环提取数组数组中的每个数组

Python 循环提取数组数组中的每个数组,python,arrays,python-3.x,numpy,Python,Arrays,Python 3.x,Numpy,我有一个数组 a = np.array([[11, 12, 13, 14], [15, 16, 17, 18], [19, 20, 21, 22]]) 我想做一个循环来提取每一行,这样最终的结果将是 a(1) = [11, 12, 13, 14] a(2) = [15, 16, 17, 18] a(3) = [19, 20, 21, 22] 然后将每个a(i)重新整形为2x2 fortran数组 a(1) = np.array([[11, 13],

我有一个数组

a = np.array([[11, 12, 13, 14],
           [15, 16, 17, 18],
           [19, 20, 21, 22]])
我想做一个循环来提取每一行,这样最终的结果将是

a(1) = [11, 12, 13, 14]
a(2) = [15, 16, 17, 18]
a(3) = [19, 20, 21, 22]
然后将每个a(i)重新整形为2x2 fortran数组

a(1) = np.array([[11, 13],
                 [12, 14])
a(2) = np.array([[15, 17],
                 [16, 18])
a(3) = np.array([[19, 21],
                 [20, 22])

使用列表理解。请注意,
a(1)
不是访问python中元素的方法。此外,索引从
0
开始。您现在可以打印
a[0]
a[1]
等等

a=[i.reshape((2,2)).T for i in a] # Update after your comment
输出

[array([[11, 13],
    [12, 14]]), array([[15, 17],
    [16, 18]]), array([[19, 21],
    [20, 22]])]
给你:

In [4]: a
Out[4]: 
array([[11, 12, 13, 14],
       [15, 16, 17, 18],
       [19, 20, 21, 22]])

In [5]: a[0]
Out[5]: array([11, 12, 13, 14])

In [6]: a[1]
Out[6]: array([15, 16, 17, 18])

In [7]: a[2]
Out[7]: array([19, 20, 21, 22])

In [9]: a[0].reshape(2,2).T
Out[9]: 
array([[11, 13],
       [12, 14]])

In [13]: for i in a:
    ...:     print(i.reshape(2,2).T)
    ...:     
    ...:     
[[11 13]
 [12 14]]
[[15 17]
 [16 18]]
[[19 21]
 [20 22]]
您可以使用for循环直接迭代它。 只需执行以下操作:

for ele in a:
    print(ele.reshape(2,2))
或者您可以使用:

for i in range(len(a)):
    print(a[i].reshape(2,2))
此外,在上述情况下,请记住,a(i)是无效的。你需要写一个[i]。

每个列表都可以使用带移位的zip。 映射是将压缩的元组转换为列表所必需的

>>>[list(map(list,zip(l,l[2:]))) for l in a]   

[[[11, 13], [12, 14]], [[15, 17], [16, 18]], [[19, 21], [20, 22]]]

感谢您的快速响应,我有一组+600数组,有没有办法制作一个循环,将每一行/数组分隔成一个[I]而不是定义每一行/数组?谢谢您的回答。但是,是否有必要创建一个循环来分隔每一行并以这种方式存储它们?作为一个[0],一个[1],…,一个[i],因为我有+600条记录,所以你想使用多少个索引?你能详细说明你想如何使用它们吗?也许我能帮上忙…谢谢你的回答。但是,是否有必要创建一个循环来分隔每一行并以这种方式存储它们?作为a[0]、a[1]、…、a[i],因为我有+600条记录,所以我更新了我的答案,这将对您有所帮助,现在您可以在列表b中执行。感谢您的回答。但是,是否有必要创建一个循环来分隔每一行并以这种方式存储它们?作为一个[0],一个[1],…,一个[i],因为我有+600条记录,所以我的答案完全符合你的意思。它存储为
a[0]
a[1]
等等。你可以把它看作是一个大架子,
[0]
[1]
等等,都是那个架子的抽屉。很抱歉给你带来了困惑。我的意思是在一个单独的数组中,[0]、[1]和[2]都是一个单独的数组。在我上面的回答中,
a[0]
是一个单独的数组。你所说的对我来说没有意义,因为
a[0]
a[1]
等都可以是单独的数组,但它们仍然属于
a
的数组或列表。我的错,你是对的。如何解决使用整形((2,2).T)时出现的“AttributeError:'tuple'对象没有属性'T'”错误?我希望结果数组以fortran方式排序。因此输出为[array([[11,13],[12,14])、array([[15,17],[16,18])、array([[19,21]、[20,22])]
>>>[list(map(list,zip(l,l[2:]))) for l in a]   

[[[11, 13], [12, 14]], [[15, 17], [16, 18]], [[19, 21], [20, 22]]]