Zastosowanie chain.from_iterable
:
vec = sp.array(list(chain.from_iterable(lst)))
ten sposób unika się stosując *
co jest dość kosztowne w obsłudze, jeśli iterable ma wiele listy zagnieżdżone.
Innym rozwiązaniem potęga być sum
listach:
vec = sp.array(sum(lst, []))
jednak pamiętać, że spowoduje to quadratic reallocation. Coś jak to wykonuje znacznie lepiej:
def sum_lists(lst):
if len(lst) < 2:
return sum(lst, [])
else:
half_length = len(lst) // 2
return sum_lists(lst[:half_length]) + sum_lists(lst[half_length:])
Na moim komputerze mam:
>>> L = [[random.randint(0, 500) for _ in range(x)] for x in range(10, 510)]
>>> timeit.timeit('sum(L, [])', 'from __main__ import L', number=1000)
168.3029818534851
>>> timeit.timeit('sum_lists(L)', 'from __main__ import L,sum_lists', number=1000)
10.248489141464233
>>> 168.3029818534851/10.248489141464233
16.422223757114615
Jak widać, prędkość-up 16x. chain.from_iterable
jest jeszcze szybsze:
>>> timeit.timeit('list(itertools.chain.from_iterable(L))', 'import itertools; from __main__ import L', number=1000)
1.905594825744629
>>> 10.248489141464233/1.905594825744629
5.378105042586658
Innym 6x speed-up.
Szukałem rozwiązania "czystego pythona", nie znając numpy. Wierzę, że rozwiązanieAbhijit unutbu/senderle to sposób, aby przejść w twoim przypadku.