【问题标题】:Python: I need to put pair of point from a list into an array [duplicate]Python:我需要将列表中的一对点放入数组中[重复]
【发布时间】:2020-03-12 15:24:27
【问题描述】:

我需要将一对点放入一个数组中

for i in range(len(lon)):
    for j in range(len(lat)):
        tab = np.array([lon[i],lat[j]])

输出:

array([1, 2])

但我希望有这个:array([1, 2],[1, 2],[1, 3],[1,2]) 原始值为:

lon = [1, 1, 1, 1] 
lat = [2, 2, 3, 2]

非常感谢

【问题讨论】:

  • 这是因为每次循环通过时tab 变量都会被覆盖...

标签: python pandas numpy


【解决方案1】:

这是因为 tab 变量在每次循环中都会被覆盖。您还可以通过列表推导更简单地做到这一点:

lon = [1,1,1,1]
lat = [2,2,3,2]

output = [[lon[i],lat[i]] for i in range(len(lon))]
print(output)

这表明:

[[1, 2], [1, 2], [1, 3], [1, 2]]

【讨论】:

  • 非常感谢,这就是我要找的东西
猜你喜欢
  • 2014-04-16
  • 1970-01-01
  • 2020-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-29
  • 1970-01-01
  • 2014-11-18
相关资源
最近更新 更多