daiyue - 1 year ago 86

Python Question

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`

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

`numpy.random.poisson(2)`

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

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)`

.

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**