-1
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: '微信'

bruce
  • 3
  • 2
  • Does this answer your question? [Store different datatypes in one NumPy array?](https://stackoverflow.com/questions/11309739/store-different-datatypes-in-one-numpy-array) – Mykola Zotko Jan 06 '20 at 08:01

1 Answers1

0

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([('微信',  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')])
Warren Weckesser
  • 110,654
  • 19
  • 194
  • 214
  • but i want to order the array by the last column – bruce Jan 06 '20 at 08:36
  • @bruce, there is nothing in the question you wrote about ordering the data, so that sounds like a new question. If you can't solve that yourself, ask a new stackoverflow question. – Warren Weckesser Jan 06 '20 at 09:59