Python “列表”对象没有“形状”属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21015674/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 21:48:03  来源:igfitidea点击:

'list' object has no attribute 'shape'

pythonlistnumpy

提问by sam

how to create an array to numpy array?

如何创建一个数组到numpy数组?

def test(X, N):
    [n,T] = X.shape
    print "n : ", n
    print "T : ", T



if __name__=="__main__":

    X = [[[-9.035250067710876], [7.453250169754028], [33.34074878692627]], [[-6.63700008392334], [5.132999956607819], [31.66075038909912]], [[-5.1272499561309814], [8.251499891281128], [30.925999641418457]]]
    N = 200
    test(X, N)

I am getting error as

我收到错误

AttributeError: 'list' object has no attribute 'shape'

So, I think I need to convert my X to numpy array?

所以,我想我需要将我的 X 转换为 numpy 数组?

采纳答案by falsetru

Use numpy.arrayto use shapeattribute.

使用numpy.array在使用shape属性。

>>> import numpy as np
>>> X = np.array([
...     [[-9.035250067710876], [7.453250169754028], [33.34074878692627]],
...     [[-6.63700008392334], [5.132999956607819], [31.66075038909912]],
...     [[-5.1272499561309814], [8.251499891281128], [30.925999641418457]]
... ])
>>> X.shape
(3L, 3L, 1L)

NOTEX.shapereturns 3-items tuple for the given array; [n, T] = X.shaperaises ValueError.

NOTEX.shape返回给定数组的 3 项元组;[n, T] = X.shape提高ValueError

回答by user2357112 supports Monica

import numpy
X = numpy.array(the_big_nested_list_you_had)

It's still not going to do what you want; you have more bugs, like trying to unpack a 3-dimensional shape into two target variables in test.

它仍然不会做你想做的事;您有更多错误,例如尝试将 3 维形状解包为test.

回答by Yura Vasiliuk

list object in python does not have 'shape' attribute because 'shape' implies that all the columns (or rows) have equal length along certain dimension.

python 中的列表对象没有 'shape' 属性,因为 'shape' 意味着所有列(或行)沿特定维度具有相等的长度。

Let's say list variable a has following properties: a = [[2, 3, 4] [0, 1] [87, 8, 1]]

假设列表变量 a 具有以下属性: a = [[2, 3, 4] [0, 1] [87, 8, 1]]

it is impossible to define 'shape' for variable 'a'. That is why 'shape' might be determined only with 'arrays' e.g.

不可能为变量“a”定义“形状”。这就是为什么“形状”可能只能用“数组”来确定,例如

b = numpy.array([[2, 3, 4]
                [0, 1, 22]
                [87, 8, 1]])

I hope this explanation clarifies well this question.

我希望这个解释能很好地澄清这个问题。

回答by Ludwig Zhou

Alternatively, you can use np.shape(...)

或者,您可以使用 np.shape(...)

For instance:

例如:

import numpy as np

import numpy as np

a=[1,2,3]

a=[1,2,3]

and np.shape(a)will give an output of (3,)

np.shape(a)会给出一个输出(3,)

回答by ZHX

if the type is list, use len(list) and len(list[0]) to get the row and column.

如果类型是列表,则使用 len(list) 和 len(list[0]) 获取行和列。

l = [[1,2,3,4], [0,1,3,4]]

len(l) will be 2 len(l[0]) will be 4

len(l) 将是 2 len(l[0]) 将是 4

回答by yunus

firstly u have to import numpy library (refer code for making a numpy array) shapeonly gives the output only if the variable is attribute of numpy library .in other words it must be a np.array or any other data structure of numpy. Eg.

首先,您必须导入 numpy 库(请参阅用于制作 numpy 数组的代码) shape仅当变量是 numpy 库的属性时才给出输出。换句话说,它必须是 np.array 或任何其他 numpy 数据结构。例如。

`>>> import numpy
>>> a=numpy.array([[1,1],[1,1]])
>>> a.shape
(2, 2)`

回答by max

?f you have list, you can print its shape as if it is converted to array

?如果你有列表,你可以打印它的形状,就好像它被转换成数组一样

import numpy as np
print(np.asarray(X).shape)