Automatic string length in recarray

前端 未结 2 987
[愿得一人]
[愿得一人] 2021-01-13 23:06

If I create a recarray in this way:

In [29]: np.rec.fromrecords([(1,\'hello\'),(2,\'world\')],names=[\'a\',\'b\'])

The result looks fine:

2条回答
  •  花落未央
    2021-01-13 23:42

    I don't know how to ask numpy to determine for you some aspects of a dtype but not others, but couldn't you have, e.g.:

    data = [(1,'hello'),(2,'world')]
    dlen = max(len(s) for i, s in data)
    st = '|S%d' % dlen
    np.rec.fromrecords(data, dtype=[('a',np.int8), ('b',st)])
    

提交回复
热议问题