有没有一种简单的方法可以从两个列表创建numpy.recarray
。例如,给出以下列表:
list1 = ["a","b","c"]
list2 = [1,2,3,4,5,6,7,8,9,10,11,12]
我想做的是得到以下结果:
rec_array = np.rec.array([('a', 1), ('a', 2),('a', 3),('a', 4),
('b', 5), ('b', 6),('b', 7),('b', 8),
('c', 9), ('c', 10),('c', 11),('c', 12)] dtype = [('string','|U5'),('int', '<i4')])
我的意思是我知道rec.array 是如何工作的,但并不真正知道如何从列表中创建一个。也许 dicts
可以让事情变得简单,因为 key ,value
选项。但是从列表中有没有办法做到这一点?
最佳答案
In [73]: list1 = ["a","b","c"]
...: list2 = [1,2,3,4,5,6,7,8,9,10,11,12]
...:
In [74]: dt = [('string','|U5'),('int', '<i4')]
简单的元素配对:
In [75]: [(i,j) for i, j in zip(list1,list2)]
Out[75]: [('a', 1), ('b', 2), ('c', 3)]
将list2
分成3组:
In [79]: list3 = [list2[i:i+4] for i in range(0,12,4)]
In [80]: list3
Out[80]: [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
双重列表理解:
In [81]: [(i,j) for i,row in zip(list1,list3) for j in row]
Out[81]:
[('a', 1),
('a', 2),
('a', 3),
('a', 4),
('b', 5),
('b', 6),
('b', 7),
('b', 8),
('c', 9),
('c', 10),
('c', 11),
('c', 12)]
从中创建一个结构化数组:
In [82]: np.array(_, dtype=dt)
Out[82]:
array([('a', 1), ('a', 2), ('a', 3), ('a', 4), ('b', 5), ('b', 6),
('b', 7), ('b', 8), ('c', 9), ('c', 10), ('c', 11), ('c', 12)],
dtype=[('string', '<U5'), ('int', '<i4')])
或者创建一个 (3,4) 数组:
In [86]: [[(i,j) for j in row] for i,row in zip(list1, list3)]
Out[86]:
[[('a', 1), ('a', 2), ('a', 3), ('a', 4)],
[('b', 5), ('b', 6), ('b', 7), ('b', 8)],
[('c', 9), ('c', 10), ('c', 11), ('c', 12)]]
In [87]: np.array(_, dt)
Out[87]:
array([[('a', 1), ('a', 2), ('a', 3), ('a', 4)],
[('b', 5), ('b', 6), ('b', 7), ('b', 8)],
[('c', 9), ('c', 10), ('c', 11), ('c', 12)]],
dtype=[('string', '<U5'), ('int', '<i4')])
In [88]: _.shape
Out[88]: (3, 4)
或者将 list1
复制到与 list2
相同的大小:
In [97]: np.array([(i,j) for i,j in zip(np.repeat(list1,4),list2)],dt).reshape(3
...: ,4)
Out[97]:
array([[('a', 1), ('a', 2), ('a', 3), ('a', 4)],
[('b', 5), ('b', 6), ('b', 7), ('b', 8)],
[('c', 9), ('c', 10), ('c', 11), ('c', 12)]],
dtype=[('string', '<U5'), ('int', '<i4')])
关于arrays - 从两个列表创建 numpy.recararray python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49959730/