【问题标题】:Error reached while attempting to create a numpy array尝试创建 numpy 数组时出错
【发布时间】:2016-06-14 18:01:09
【问题描述】:

这是我的代码:

import numpy as np
x = np.array[[1,2]]
print x

这是输出:

Traceback (most recent call last):
  File "Shear_Moment_Test.py", line 2, in <module>
    x = np.array[[1,2]]
TypeError: 'builtin_function_or_method' object has no attribute '__getitem__'

任何建议将不胜感激!

【问题讨论】:

  • np.array 是一个函数,所以试试np.array([1,2])

标签: python arrays numpy


【解决方案1】:

您使用的语法错误。用这个 x = np.array([1,2])

【讨论】:

  • 谢谢!那行得通。我在添加另一行时遇到问题:x = np.array([1,2],[3,4]) 产生 Traceback(最近一次调用最后):文件“Shear_Moment_Test.py”,第 10 行,在 x = np.array([1,2],[3,4]) TypeError: data type not understand 我是否错误地添加了附加数据?
  • 评论和近距离投票可能更合适,因为错误只是由于拼写错误。我投票关闭。请在您的其他问题中发布一个新问题,或先查看docs.scipy.org/doc/numpy-dev/user/quickstart.html
  • @Benjamin 我不认为这是一个错字。这是缺乏理解。 @Matt Moniot 当您执行 np.array([1, 2], [3, 4]) 时,您将为 np.array 函数提供两个列表。然而np.array 的第二个位置参数是dtype。因此,您会收到错误 data type not understood。对于二维数组,您需要传递一个列表列表:np.array([[1, 2], [3, 4]])
  • 谢谢。我对 StackOverflow 不是很熟悉。我会照你说的做。
  • @MaxNoe - 谢谢!我遵循的示例只有 1 行。我没有意识到它的语法略有不同。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-08
  • 2012-04-24
  • 1970-01-01
  • 1970-01-01
  • 2022-01-01
  • 1970-01-01
相关资源
最近更新 更多