关于如何在Python中重复数组中某个值的任何建议?例如,我想在array_a
中只重复2次:
array_a = [1, 2, 1, 2, 1, 1, 2]
想要的结果是:我重复每个2
并离开1
:
array_a = [1, 2, 2, 1, 2, 2, 1, 1, 2, 2] # only the `2` should be repeated
我尝试了numpy
,我可以复制整个数组但不是一定值。
如果你对一个numpy解决方案感兴趣,你可以使用np.repeat
重复一个数组。
>>> import numpy as np
>>> np.repeat(array_a, array_a)
array([1, 2, 2, 1, 2, 2, 1, 1, 2, 2])
仅当数据中包含1和2时,此方法才有效。对于通用解决方案,请考虑
>>> n_repeats = 2
>>> temp = np.where(np.array(array_a) == 2, n_repeats, 1)
>>> np.repeat(array_a, temp)
array([1, 2, 2, 1, 2, 2, 1, 1, 2, 2])
可能是你可以使用dictionary
每个独特的元素和它需要重复的次数。然后使用list comprehension
创建数组:
array_a = [1,2,1,2,1,1,2]
repeat_times = {1:1, 2:2} # 1 is 1 time and 2 is repeated two times
result = [i for i in array_a for j in range(repeat_times[i])]
print(result)
输出:
[1, 2, 2, 1, 2, 2, 1, 1, 2, 2]
这似乎是generator的一个很好的用例:
>>> def repeater(iterable, repeat_map):
... for value in iterable:
... for i in range(repeat_map.get(value, 1)):
... yield value
...
>>> array_a = [1,2,1,2,1,1,2]
>>> list(repeater(array_a, repeat_map={2: 2}))
[1, 2, 2, 1, 2, 2, 1, 1, 2, 2]
如果将其转换为列表,则可以遍历它,如果符合您的条件,则添加一个额外版本。例如:
a = [1,2,1,2,1,1,2]
long_a = []
for x in a:
long_a.append(x)
if x == 2:
long_a.append(x)
https://docs.python.org/3/reference/compound_stmts.html#for
https://docs.python.org/2/tutorial/datastructures.html#more-on-lists
尝试使用理解。
array = [1, 2, 1, 2, 1, 1, 2]
element_to_repeat = 2
result = [
repeats_element
for repeats in
((element,)*2 if element == element_to_repeat else (element,) for element in array)
for repeats_element in repeats
]
它基本上吐出元组,“重复”,如果它不是要重复的元素,则包含元素一次,如果它是要重复的元素,则包含两次元素。然后,这些“重复”元组的所有元素都变成了答案。
使用发电机。
array = [1, 2, 1, 2, 1, 1, 2]
element_to_repeat = 2
def add_repeats(array, element_to_repeat):
for element in array:
if element == element_to_repeat:
yield element
yield element
else:
yield element
result = list(add_repeats(array, element_to_repeat))
这是一个方便的单行使用itertools
和列表理解与if和其他。首先它创建一个嵌套列表(能够在某个位置重复项目),然后使用.chain()
方法将其简化为最终:
from itertools import chain
array_a = [1, 2, 1, 2, 1, 1, 2]
list(chain.from_iterable([[item, item] if item == 2 else [item] for item in array_a]))
[1, 2, 2, 1, 2, 2, 1, 1, 2, 2] # output
double的具体值在if语句中。使用乘数(而不是[item, item]
)和变量(而不是2
)会使这更容易更通用,例如:
from itertools import chain
def repeat_certain_value(array, val, n):
return list(chain.from_iterable(([i] * n if i == val else [i] for i in array)))
repeat_certain_value([1, 2, 1, 2, 1, 1, 2], 2, 2)
[1, 2, 2, 1, 2, 2, 1, 1, 2, 2] # output
repeat_certain_value([0, -3, 1], -3, 5)
[0, -3, -3, -3, -3, -3, 1] # output
虽然这种方法使用内置库是一个方便的单线程,但coldspeed的方法更快:
%timeit for x in range(1000): repeat_certain_value([1, 1, 1, 2, 2, 2, 3, 3, 3] * 100, 2, 2)
10 loops, best of 3: 165 ms per loop
%timeit for x in range(1000): coldspeeds_solution([1, 1, 1, 2, 2, 2, 3, 3, 3] * 100, 2, 2)
10 loops, best of 3: 100 ms per loop
可以尝试列表理解并创建一个flat
函数:
array_a = [1, 2, 1, 2, 1, 1, 2]
def flat(l):
newl=[]
for i in l:
if isinstance(i,list):
newl.extend(i)
else:
newl.append(i)
return newl
print(flat([[i]*2 if i==2 else i for i in array_a]))
输出:
[1, 2, 2, 1, 2, 2, 1, 1, 2, 2]