【问题标题】:i want to create a 2D array which consist of nodes name and its x y coordinate using numpy python我想使用 numpy python 创建一个由节点名称及其 x y 坐标组成的二维数组
【发布时间】:2019-03-14 22:04:10
【问题描述】:

我想为不询问节点的输入创建一个二维数组,并为每个节点从用户接受 2 个值 x 坐标和 y 坐标并生成其二维数组,其中包含 3 列节点、x 坐标、y 坐标。

输入:

enter the number of nodes:3
10 12
12 13
5 6 

输出

[A 10 12
 B 12 13
 C 5 6 ]

【问题讨论】:

  • 具有这 3 个值的 3 个元素列表的列表是更好的结构。

标签: python python-3.x numpy


【解决方案1】:

定义2个函数:

def foo(node):
    return [node]+[int(i) for i in input('for node: %s: '%node).split()]

def foobar():
    n = input('enter the number of nodes:')
    n = int(n)
    alist=[]
    for i,n in zip(range(n),'ABCDEF'):
        alist.append(foo(n))
    return alist

并根据您的输入获取列表列表:

In [348]: foobar()
enter the number of nodes:3
for node: A: 10 12
for node: B: 12 13
for node: C: 5 6
Out[348]: [['A', 10, 12], ['B', 12, 13], ['C', 5, 6]]

节点名称字符串和数字的混合不能构成一个好的 numpy 数组。

它可以变成一个字符串数组。或 object dtype 数组。或结构化数组。或者更好的是,一个节点名称数组和另一个只有 2 列的 2d 坐标。

【讨论】:

    猜你喜欢
    • 2018-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-04
    • 1970-01-01
    • 2019-09-23
    • 1970-01-01
    相关资源
    最近更新 更多