[Numpy-discussion] Joining lists to an array

Florian Lindner mailinglists at xgm.de
Mon Dec 2 11:52:07 EST 2013


I have this piece of example code

import random, numpy as np

y = []
doc_all = []
# da = np.zeros(2)
for i in range(4): 
    docs = range(random.randint(1, 10))
    y += [i]*len(docs)
    doc_all += docs
    # np.append(da, np.column_stack((docs, y)), axis=0)
data = np.array([doc_all, y]).transpose()

y and docs are lists that are created in the loop body and all joined 
together. From these two long lists an array is created at the end.

At the end data has a shape like (28, 2).

Is there a way I can do this more elegantly using numpy or scipy tricks? I was 
working on something like the two lines I commented out, but ...


More information about the NumPy-Discussion mailing list