如何复制列表/数组中的特定值?

As *_*ent 7 python arrays numpy list numpy-ndarray

关于如何在Python中重复数组中某个值的任何建议?例如,我想只重复2次array_a:

array_a = [1, 2, 1, 2, 1, 1, 2]
Run Code Online (Sandbox Code Playgroud)

想要的结果是:我重复每一个2并离开1:

array_a = [1, 2, 2, 1, 2, 2, 1, 1, 2, 2]  # only the `2` should be repeated
Run Code Online (Sandbox Code Playgroud)

我试过numpy,我可以复制整个数组,但不是一定的值.

小智 0

  1. 循环数组(Python 中的“列表”)

  2. 找到号码

  3. 获取匹配数字在数组中的位置

  4. 在每个匹配位置后插入另一个数字

https://docs.python.org/3/reference/compound_stmts.html#for

https://docs.python.org/2/tutorial/datastructs.html#more-on-lists