簡體   English   中英

重塑包含 Python 中的數組的列表

[英]Reshaping a list containing an array in Python

我想將列表Ii重塑為(1,11,2) ,但出現錯誤。 我提出了預期的 output。

import numpy as np

Ii=[np.array([[0, 2],
        [2, 4],
        [2, 5],
        [2, 6],
        [3, 1],
        [3, 7],
        [4, 5],
        [4, 6],
        [5, 3],
        [5, 7],
        [6, 5]])]

Y=Ii[0].shape
Ii=Ii[0].reshape(1,Y)

錯誤是

in <module>
    Ii=Ii[0].reshape(1,Y)

TypeError: 'tuple' object cannot be interpreted as an integer

預期的 output 是

array([[[0, 2],
        [2, 4],
        [2, 5],
        [2, 6],
        [3, 1],
        [3, 7],
        [4, 5],
        [4, 6],
        [5, 3],
        [5, 7],
        [6, 5]]])

np.newaxis可以在這里用來重塑數組。

Ii[0][np.newaxis,:]

或者您可以在unpacking元組后使用reshaping

Ii[0].reshape([1, *Y])

嘗試這個:

Ii = Ii[0].reshape(1, Y[0], Y[1])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM