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

1
  • Please indicate how much faster you need for your application. Commented Sep 12, 2018 at 5:58

4 Answers 4

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
Sign up to request clarification or add additional context in comments.

Comments

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]

Comments

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]]

Comments

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.

1 Comment

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 very helpful. Please edit your answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.