带numpy的数组

2024-06-07 05:21:39 发布

您现在位置:Python中文网/ 问答频道 /正文

你好,我想有一个这样的数组(1):

import numpy as np
A = np.array([ (1, 2, 9.799, 4.7, 4.77, 148929.0, 450030016.0),
(11, 21, 91.799, 41.7, 41.77, 1489129.0, 4500130016.0),
(41, 25, 93.799, 74.7, 94.77, 1487929.0, 4500340016.0)],
dtype = [('a', '<i4'), ('z', '<f4'), ('e', '<f4'), ('r', '<f4'), ('t', '<f4'), ('y', '<f4'), ('u', '<f4')])

使用此项:

A = np.array([])
A = np.append(A,(1, 2, 9.799, 4.7, 4.77, 148929.0, 450030016.0))
A = np.append(A,(11, 21, 91.799, 41.7, 41.77, 1489129.0, 4500130016.0))
A = np.append(A,(41, 25, 93.799, 74.7, 94.77, 1487929.0, 4500340016.0))

但问题是我只能

array([ 1, 2, 9.799, 4.7, 4.77, 148929.0, 450030016.0,
11, 21, 91.799, 41.7, 41.77, 1489129.0, 4500130016.0,
41, 25, 93.799, 74.7, 94.77, 1487929.0, 4500340016.0  ])

没有像(1)那样的括号,我也不知道如何修复数据类型这样做

非常感谢你的帮助


Tags: importnumpyasnp数组array括号数据类型
1条回答
网友
1楼 · 发布于 2024-06-07 05:21:39

使用此选项:

l = []
l.append((1, 2, 9.799, 4.7, 4.77, 148929.0, 450030016.0))
l.append((11, 21, 91.799, 41.7, 41.77, 1489129.0, 4500130016.0))
l.append((41, 25, 93.799, 74.7, 94.77, 1487929.0, 4500340016.0))
A = np.array(l, dtype=[('a', '<i4'), ('z', '<f4'), ('e', '<f4'), ('r', '<f4'), ('t', '<f4'), ('y', '<f4'), ('u', '<f4')])

相关问题 更多 >