如何在np.asarray中设置dtype?

huangapple go评论71阅读模式
英文:

How to set dtype in np.asarray?

问题

list_t = [['微信', '61860', '63409', '-1549'],
 ['支付宝', '121990', '112995', '9239'],
 ['腾讯视频', '116221', '106665', '7955'],
 ['讯飞语音引擎', '350777', '325049', '51728'],
 ['百度', '140252', '108637', '32015'],
 ['抖音短视频', '118262', '150556', '31706'],
 ['QQ', '118878', '104456', '14452'],
 ['高德地图', '95748', '82388', '12685'],
 ['手机淘宝', '148342', '135533', '12519'],
 ['拼多多', '335259', '224793', '111166']]
dt = np.dtype([("a","U7"),("b","i4"),("c","i4"),("d","i4")])
array_t = np.asarray(list_t, dtype=dt)
英文:
list_t = [['微信', '61860', '63409', '-1549'],
 ['支付宝', '121990', '112995', '9239'],
 ['腾讯视频', '116221', '106665', '7955'],
 ['讯飞语音引擎', '350777', '325049', '51728'],
 ['百度', '140252', '108637', '32015'],
 ['抖音短视频', '118262', '150556', '31706'],
 ['QQ', '118878', '104456', '14452'],
 ['高德地图', '95748', '82388', '12685'],
 ['手机淘宝', '148342', '135533', '12519'],
 ['拼多多', '335259', '224793', '111166']]
dt = np.dtype([("a","U7"),("b","i4"),("c","i4"),("d","i4")])
array_t = np.asarray(list_t, dtype=dt)

ValueError: invalid literal for int() with base 10: '微信'

答案1

得分: 0

要创建一个结构化数组,与结构化元素关联的值必须存储为元组,而不是列表:

In [62]: array_t = np.asarray([tuple(row) for row in list_t], dtype=dt)                                            

In [63]: array_t                                                                                                   
Out[63]: 
array([('微信',  61860,  63409,  -1549), ('支付宝', 121990, 112995,   9239),
       ('腾讯视频', 116221, 106665,   7955),
       ('讯飞语音引擎', 350777, 325049,  51728), ('百度', 140252, 108637,  32015),
       ('抖音短视频', 118262, 150556,  31706), ('QQ', 118878, 104456,  14452),
       ('高德地图',  95748,  82388,  12685), ('手机淘宝', 148342, 135533,  12519),
       ('拼多多', 335259, 224793, 111166)],
      dtype=[('a', '<U7'), ('b', '<i4'), ('c', '<i4'), ('d', '<i4')])
英文:

To create a structured array, the values associated with a structured element must be stored as tuples, not lists:

In [62]: array_t = np.asarray([tuple(row) for row in list_t], dtype=dt)                                            

In [63]: array_t                                                                                                   
Out[63]: 
array([(&#39;微信&#39;,  61860,  63409,  -1549), (&#39;支付宝&#39;, 121990, 112995,   9239),
       (&#39;腾讯视频&#39;, 116221, 106665,   7955),
       (&#39;讯飞语音引擎&#39;, 350777, 325049,  51728), (&#39;百度&#39;, 140252, 108637,  32015),
       (&#39;抖音短视频&#39;, 118262, 150556,  31706), (&#39;QQ&#39;, 118878, 104456,  14452),
       (&#39;高德地图&#39;,  95748,  82388,  12685), (&#39;手机淘宝&#39;, 148342, 135533,  12519),
       (&#39;拼多多&#39;, 335259, 224793, 111166)],
      dtype=[(&#39;a&#39;, &#39;&lt;U7&#39;), (&#39;b&#39;, &#39;&lt;i4&#39;), (&#39;c&#39;, &#39;&lt;i4&#39;), (&#39;d&#39;, &#39;&lt;i4&#39;)])

huangapple
  • 本文由 发表于 2020年1月6日 15:55:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/59608498.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定