2

I am wondering how to duplicate each element in a list arbitrary of times, e.g.

l = ['a', 'b', 'c']

the duplicate elements in l result in a new list,

n = ['a', 'a', 'a', 'a', 'b', 'b', 'c', 'c', 'c']

so 'a' has been duplicated 3 times, 'b' once, 'c' twice. The number of duplicates for each element are decided by numpy.random.poisson e.g. numpy.random.poisson(2).

3 Answers 3

3

Here's a NumPy based vectorized approach using np.repeat to create an array -

np.repeat(l, np.random.poisson([2]*len(l)))

If you need a list as output, append .tolist() there -

np.repeat(l, np.random.poisson([2]*len(l))).tolist()

If you would like to keep at least one entry for each element, add a clipping there with np.random.poisson([2]*len(arr)).clip(min=1).

Sign up to request clarification or add additional context in comments.

2 Comments

I just realized that np.random.poisson(2) may equal to 0, but I like to keep each element in the list, some elements may be missing if np.random.poisson([2]) is 0, how to avoid it?
@daiyue Added a note to avoid such a case.
1

Multiply each element in the list with the value returned from numpy.random.poisson(2), join it and then feed it to list:

r = list(''.join(i*random.poisson(2) for i in l))

For one run, this randomly results in:

['a', 'b', 'b', 'b', 'b', 'b', 'b', 'c', 'c', 'c']

Since you use np either way, I'd go with Divakar's solution (which, for lists larger than your example, executes faster).

Comments

1
>>> l = ['a', 'b', 'c']
>>> n = []
>>> for e in l:
...     n.extend([e] * numpy.random.poisson(2))
... 
>>> n
['a', 'a', 'b', 'c']

Comments

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.