首页 > 解决方案 > 从单行输入创建坐标列表(x、y、z 变量)

问题描述

在这里我必须从单个输入行中提取坐标

输入有两部分:首先是点或坐标的数量,然后是每个坐标,用逗号分隔

输入:

2

1,2,3,4,5,6

预期输出:[[1,2,3],[4,5,6]]

到目前为止,这就是我想出的:

import numpy as np 

def CreateList(string):
  return list(int(i) for i in string.split(','))

num=input("enter number of cordinates")
string=input("enter the cordinates")
cordinates=CreateList(string)
CordinatesList = np.array(cordinates).reshape(num, 3) 
print(CordinatesList)

这段代码给了我一个 numpy 实现的错误:

"CordinatesList = np.array(cordinates).reshape(num, 3)" 
"TypeError: 'str' object cannot be interpreted as an integer"

repl 链接my_code

标签: pythonlistnumpy

解决方案


推荐阅读