Чтобы использовать соединение dtype
, функция должна возвращать кортежи, а не списки
In [977]: def foo(bar):
...: return (bar,) * 3 # so for 4 it returns [4,4,4], ..
...:
...: a = [1,2,3,4,5,6,7]
...: b = map(foo,a)
In [978]: list(b)
Out[978]: [(1, 1, 1), (2, 2, 2), (3, 3, 3), (4, 4, 4), (5, 5, 5), (6, 6, 6), (7, 7, 7)]
In [979]: def foo(bar):
...: return (bar,) * 3 # so for 4 it returns [4,4,4], ..
...:
...: a = [1,2,3,4,5,6,7]
...: b = map(foo,a)
In [980]: np.fromiter(b, 'i,i,i')
Out[980]:
array([(1, 1, 1), (2, 2, 2), (3, 3, 3), (4, 4, 4), (5, 5, 5), (6, 6, 6),
(7, 7, 7)], dtype=[('f0', '<i4'), ('f1', '<i4'), ('f2', '<i4')])
некоторые периоды времени:
In [981]: %%timeit b = map(foo,a)
...: np.array(list(b))
...:
...:
1.9 µs ± 55.2 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
In [982]: %%timeit b = map(foo,a)
...: np.fromiter(b, 'i,i,i')
...:
...:
17.2 µs ± 9.72 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)