python - 问题 dtyping 3 维结构化数组
问题描述
初学者在这里!我正在尝试为 3 维数组的每个不同行关联不同的类型。我尝试了一个扩展的,我用 2 维做的,但由于某种未知的原因,它一直给我一个 TypeError:数据类型不理解。我需要为大学项目确定的大量字符串列表,感谢您的帮助。我使用最新的 python 和 numpy 包。:)
dt = np.array([[('ID', 'i4'),('Name', 'U10')],
[('ID', 'i4'),('Name', 'U10')],
[('ID', 'i4'),('Name', 'U10')]])
Gthm_Extend_id = np.array([[(0, 'Red'), ## Colors
(1, 'Orange'),
(2, 'Yellow'),
(3, 'Green'),
(4, 'Blue'),
(5, 'Purple'),
(6, 'Brown'),
(7, 'Black')],
[(0, 'Defender'), ## Keywords
(1, 'First Strike'),
(2, 'FLying'),
(3, 'Haste'),
(4, 'Hexproof'),
(5, 'Indestructible'),
(6, 'Lifelink'),
(7, 'Trample'),
(8, 'Taunt')],
[(0, 'Devils'), ## Tribe[enter image description here][1]s
(1, 'Whales'),
(2, 'Seraphins'),
(3, 'Beasts'),
(4, 'Turtles'),
(5, 'Octopuses'),
(6, 'Ogres'),
(7, 'Horrors')]],
dtype = dt)
解决方案
In [327]: dt = np.dtype([('ID', 'i4'),('Name', 'U10')])
In [328]: np.array([[(0, 'Red'), ## Colors
...: (1, 'Orange'),
...: (2, 'Yellow'),
...: (3, 'Green'),
...: (4, 'Blue'),
...: (5, 'Purple'),
...: (6, 'Brown'),
...: (7, 'Black')],
...: [(0, 'Defender'), ## Keywords
...: (1, 'First Strike'),
...: (2, 'FLying'),
...: (3, 'Haste'),
...: (4, 'Hexproof'),
...: (5, 'Indestructible'),
...: (6, 'Lifelink'),
...: (7, 'Trample'),
...: ],
...: [(0, 'Devils'), ## Tribe[enter image description here][1]s
...: (1, 'Whales'),
...: (2, 'Seraphins'),
...: (3, 'Beasts'),
...: (4, 'Turtles'),
...: (5, 'Octopuses'),
...: (6, 'Ogres'),
...: (7, 'Horrors')]],
...: dt
...: )
Out[328]:
array([[(0, 'Red'), (1, 'Orange'), (2, 'Yellow'), (3, 'Green'),
(4, 'Blue'), (5, 'Purple'), (6, 'Brown'), (7, 'Black')],
[(0, 'Defender'), (1, 'First Stri'), (2, 'FLying'), (3, 'Haste'),
(4, 'Hexproof'), (5, 'Indestruct'), (6, 'Lifelink'),
(7, 'Trample')],
[(0, 'Devils'), (1, 'Whales'), (2, 'Seraphins'), (3, 'Beasts'),
(4, 'Turtles'), (5, 'Octopuses'), (6, 'Ogres'), (7, 'Horrors')]],
dtype=[('ID', '<i4'), ('Name', '<U10')])
In [329]: _.shape
Out[329]: (3, 8)
推荐阅读
- python - 使用 Selenium 进行 Web 抓取:属性 page_souce 错误太大?
- javascript - 如何从 index.js 中获取 TS 文件?
- javascript - 为什么我在使用 axios.post() 时收到此 404 错误?使用 Express/Node?
- python - 相同输入数据上的相同代码具有不同的输出
- javascript - mongo 在节点中使用 mongoose 想要在查询中使用 or 和
- c# - 如何在字符串生成器中转换流
- java - 使用 regExp [Java] 返回子字符串
- fusionauth - Fusionauth 本身是身份提供者吗?
- c# - 什么模式可以用来创建一个类的对象,但以不同的方式填充它的属性?
- rest - 如果我在邮递员上传递了正确的 channelID keyID 和 secret,则在苹果新闻上获取 "code": "WRONG_SIGNATURE" 创建 api enen