0

I try to find a faster way to create such a list:

import numpy as np
values = [0,1,2]
repeat = [3,4,2]
list = np.empty(0, dtype=int)
for i in range(len(values)):
    list = np.append(list, np.full(repeat[i], values[i]))
print list

returns

[0 0 0 1 1 1 1 2 2]

Any idea? Thanks

4 Answers4

0

You can save a lot of time using native python lists instead of numpy arrays. When I ran your code using the timeit module, it took 16.87 seconds. The following code took 0.87.

list = []
for val, rep in zip(values, repeat):
    list.extend([val]*rep)

If you then convert list to a numpy array using list = np.array(list), that time goes up to 2.09 seconds.

Of course, because numpy is optimized for large amounts of data, this may not hold for very long lists of values with large numbers of repeats. In this case, one alternative would be to do you memory allocation all at the same time, instead of continually lengthening the array (which I believe covertly causes a copy to made, which is slow). The example below completes in 4.44 seconds.

list = np.empty(sum(repeat), dtype=int) #allocate the full length
i=0 #start the index at 0
for val, rep in zip (values, repeat):
    list[i:i+rep] = [val]*rep #replace the slice
    i+=rep #update the index
acattle
  • 3,073
  • 1
  • 16
  • 21
0

You can try this. Multiply lists of values by lengths for each pair of values and lengths.
You will get list of lists

L = [[i]*j for i, j in zip(values, repeat)] 
print(L)

returns

[[0, 0, 0], [1, 1, 1, 1], [2, 2]]

Than make a flat list

flat_L = [item for sublist in L for item in sublist] 
print(flat_L)
[0, 0, 0, 1, 1, 1, 1, 2, 2]
Einar A
  • 141
  • 1
  • 1
  • 9
0

I would do like this:

a=[1,2,3]
b=[2,4,3]
x=[[y]*cnt_b for cnt_b,y in zip(b,a)]

Output:

[[1,1],[2,2,2,2],[3,3,3]]

Dragos Vasile
  • 453
  • 4
  • 12
0
In [8]: [i for i, j in zip(values, repeat) for _ in range(j)]
Out[8]: [0, 0, 0, 1, 1, 1, 1, 2, 2]

Here, we are zipping values and repeat together with zip to have one to one correspondence between them (like [(0, 3), (1, 4), (2, 2)]). Now, in the list comprehension I'm inserting i or values and looping them over range of j to repeat it jth times.

Osman Mamun
  • 2,864
  • 1
  • 16
  • 22
  • 1
    While this might answer the authors question, it lacks some explaining words and links to documentation. Raw code snippets are not very helpful without some phrases around it. You may also find [how to write a good answer](https://stackoverflow.com/help/how-to-answer) very helpful. Please edit your answer. – hellow Sep 12 '18 at 08:25