Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python元组数组_Python_Arrays_For Loop_Numpy_Tuples - Fatal编程技术网

Python元组数组

Python元组数组,python,arrays,for-loop,numpy,tuples,Python,Arrays,For Loop,Numpy,Tuples,我试图用python编写一个for循环来创建一个元组数组 Output = [(0.0,C[0]),(0.0,C[1]),(0.0,C[2]), .... , (0.0,C[n-1])] 其中C是一些其他数字的数组 如果我尝试(与matlab一起工作的东西) 然后我得到一个错误ValueError:用序列设置数组元素 有人能帮忙吗?怎么样: numpy.array([(0.0, C[x]) for x in range(n)]) output = np.zeros((N, 2), dtype

我试图用python编写一个for循环来创建一个元组数组

Output = [(0.0,C[0]),(0.0,C[1]),(0.0,C[2]), .... , (0.0,C[n-1])]
其中
C
是一些其他数字的数组

如果我尝试(与matlab一起工作的东西)

然后我得到一个错误
ValueError:用序列设置数组元素

有人能帮忙吗?

怎么样:

numpy.array([(0.0, C[x]) for x in range(n)])
output = np.zeros((N, 2), dtype='float')
output[:, 1] = C[:N]
顺便说一句,“元组数组”对我来说没有多大意义。如果你指的是一个对象数组(可能是matlab中的单元数组?),我建议使用列表而不是数组。

怎么样:

output = np.zeros((N, 2), dtype='float')
output[:, 1] = C[:N]

顺便说一句,“元组数组”对我来说没有多大意义。如果你指的是一个对象数组(在matlab中可能是一个单元格数组?),我建议使用列表而不是数组。

看看结构化数组(也称为“记录数组”):

您可以定义以下内容:

>>> x = np.zeros((2,),dtype=('i4,f4,a10'))
>>> x[:] = [(1,2.,'Hello'),(2,3.,"World")]
>>> x
array([(1, 2.0, 'Hello'), (2, 3.0, 'World')],
     dtype=[('f0', '>i4'), ('f1', '>f4'), ('f2', '|S10')])

看看结构化数组(也称为“记录数组”):

您可以定义以下内容:

>>> x = np.zeros((2,),dtype=('i4,f4,a10'))
>>> x[:] = [(1,2.,'Hello'),(2,3.,"World")]
>>> x
array([(1, 2.0, 'Hello'), (2, 3.0, 'World')],
     dtype=[('f0', '>i4'), ('f1', '>f4'), ('f2', '|S10')])

如果
n
是数组的长度
C
,则只
numpy.array([(0.0,C)表示C中的C])
如果
n
是数组的长度
C
,则只
numpy.array([(0.0,C)表示C中的C])