有什么建议可以在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
,我可以复制整个数组,但无法复制特定的值。有什么建议可以在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])
>>> 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])
你可以使用字典
来记录每个唯一元素以及它需要重复的次数。然后使用列表推导式
来创建数组:
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]
j
这样的虚拟变量使用 _
。 - Denziloea = [1,2,1,2,1,1,2]
long_a = []
for x in a:
long_a.append(x)
if x == 2:
long_a.append(x)
list
是不必要的。 - Denziloe>>> 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]
尝试使用推导式。
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
]
这里有一个方便的一行代码,使用itertools
和包含if和else的列表推导式。首先它创建了一个嵌套列表(以具有在特定位置重复项目的能力),然后最后使用.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
要加倍的特定值在 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
使用生成器。
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))
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]
遍历数组(在Python中称为“列表”)
查找数字
获取匹配数字在数组中的位置
在每个匹配位置之后插入另一个数字
https://docs.python.org/3/reference/compound_stmts.html#for
https://docs.python.org/2/tutorial/datastructures.html#more-on-lists