【问题标题】:How to add elements of an array to a numpy array to represent coordinates?如何将数组的元素添加到 numpy 数组以表示坐标?
【发布时间】:2022-01-18 11:58:30
【问题描述】:

大家好 Stackoverflow, 我想实现以下程序。 我有这两个数组:

a = np.array([175, 370])
b = [array([175, 176, 176, 176]), array([371, 369, 370, 371])]

b数组的元素应该代表一些点的坐标

b = [array[x1, x2, x3, x4], array[y1, y2, y3, y4]]

我想将 'b' 数组的元素添加到 'a' 数组中。这样我就有了这样的新“a”数组:

a = [[175, 370], [175, 371], [176, 369], [176, 370], [176, 371]]
so the 'a' array has the following meaning 
a = [(x0, y0), (x1, y1), (x2, y2), (x3, y3), (x4, y4)]

我应该使用哪个 numpy 函数来实现这一点?

【问题讨论】:

  • a 和 b 数组定义如下 a = np.array([175, 370])b = [array([175, 176, 176, 176]), array([371, 369 , 370, 371])]

标签: python arrays numpy


【解决方案1】:

您可以尝试zip 函数将b 中的两个数组配对并将其附加到a

import numpy as np

a = [175, 370]
b = [np.array([175, 176, 176, 176]), np.array([371, 369, 370, 371])]

a = [a] # a will be changed from list of number to list of paired numbers
a += [list(c) for c in zip(b[0], b[1])]

【讨论】:

  • 感谢您的回复 (y)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多