【问题标题】:Convert list to vector in python在python中将列表转换为向量
【发布时间】:2019-01-29 23:20:54
【问题描述】:

我想将一个长列表转换为矩阵或向量,以便将其转换为度数。我不想使用“for”循环,因为列表太长,我希望我的程序尽可能快。我怎么能这样做?

编辑:

这是我迄今为止尝试过的,

latR=np.asarray(lat)
latR=np.degrees(latR)

但这就是我得到的:

ufunc 'degrees' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

谢谢!

【问题讨论】:

  • 欢迎来到 SO。当您将错误复制/粘贴到 Google 中时,您发现了什么?
  • 欢迎来到 StackOverflow,首先建议添加“numpy”标签以使您的问题更具体,然后您能告诉我们错误显示在哪一行吗?因为我认为在第二行(np.concatenate...)但不确定。
  • 错误提示lat包含非数字数据,或者numpy不知道如何处理。您能否包含一个简短的数据示例? (遵循stackoverflow.com/help/mcve 中的指南)
  • 你在asarray函数之后看latR了吗? dtype 是什么? shape?

标签: python list numpy matrix vector


【解决方案1】:

你可以直接在你的初始名单上做np.degrees(lat)

“向量”只是一个一维数组(第一行得到的)。 concatenate 将新元素添加到您的数组中,因此这里不需要。所以,你可以省略第二行,但甚至没有必要这样做。直接在列表中使用degrees

【讨论】:

  • 我只尝试了 np.degrees(lat),但收到此错误消息:输入类型不支持 ufunc 'degrees',并且根据强制转换规则“安全”
  • 您的列表中有一些非数字值
【解决方案2】:

原来我的列表是由字符串组成的,而不是我假设的浮点数......这就是转换所需的全部内容,然后绘制所需的内容:

latD = np.degrees(latitude)              
lonD = np.degrees(longitude)             

xs, ys = m(lonD, latD)
m.plot(xs, ys,linewidth = 3,color = 'r')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-08
    • 2011-07-07
    • 2012-07-17
    • 2021-12-25
    相关资源
    最近更新 更多