我有一个包含重复项的整数列表。示例:
37 1 30 38 5 39 5 5 5 40 33 5 35 42 25 36 27 27 43 27
我需要将重复的号码更改为其他一些号码,如果它们不相继出现。新数字不应与列表中的其他数字重复。例如,上面的列表应如下所示:
37 1 30 38 5 39 8 8 8 40 33 2 35 42 25 36 27 27 43 55
这就是我得到的:
a = [37, 1, 30, 38, 5, 39, 5, 5, 5, 40, 33, 5, 35, 42, 25, 36, 27, 27, 43, 27]
duplicates = list(item for item, count in Counter(a).items() if count > 1)
for dup in duplicates:
positions = []
for item in range(len(a)):
if a[item] == dup:
positions.append(item)
for x in range(len(positions)-1):
if positions[x+1] - positions[x] != 1:
ran = random.randrange(1, len(a))
while ran in a:
ran = random.randrange(1, len(a))
a[positions[x+1]] = ran
else:
y = x
while positions[y+1] - positions[y] == 1:
a[positions[y+1]] = a[positions[y]]
y += 1
[37,1,30,38, 5 ,39, 17 , 17 , 17 , 40、33, 13 ,35、42、25、36, 27 , 27 , 43, 8 ]
但是我认为这不是一个好的解决方案。
答案 0 :(得分:2)
您可以使用itertools.groupby
以相同编号的块处理列表,并使用带有itertools.count
的生成器表达式生成替换编号:
input_list = [37, 1, 30, 38, 5, 39, 5, 5, 5, 40, 33, 5, 35, 42, 25, 36, 27, 27, 43, 27]
import itertools
# make a generator that yields unused numbers
input_values = set(input_list)
unused_number = (num for num in itertools.count() if num not in input_values)
# loop over the input, grouping repeated numbers, and keeping track
# of numbers we've already seen
result = []
seen = set()
for value, group in itertools.groupby(input_list):
# if this number has occurred already, pick a new number
if value in seen:
value = next(unused_number)
# remember that we've seen this number already so future
# occurrences will be replaced
seen.add(value)
# for each repeated number in this group, add the number
# to the output one more time
for _ in group:
result.append(value)
print(result)
# output:
# [37, 1, 30, 38, 5, 39, 0, 0, 0, 40, 33, 2, 35, 42, 25, 36, 27, 27, 43, 3]
答案 1 :(得分:1)
只是另一种方法。
演示:
import random
L = [37, 1, 30, 38, 5, 39, 5, 5, 5, 40, 33, 5, 35, 42, 25, 36, 27, 27, 43, 27]
result = []
previous_value = ''
dup_val = ''
length = len(L)
ran = ''
for i, v in enumerate(L): #Iterate Each element with index.
if (v not in result) or (v == previous_value): #Check if value in result or previous value is the same.
result.append(v)
previous_value = v
dup_val = ''
else:
if dup_val == v: #Check if previous value is duplicate
result.append(ran)
continue
else:
ran = random.randrange(1, length)
while ran in result:
ran = random.randrange(1, length)
result.append(ran)
dup_val = v
print(result)
输出:
[37, 1, 30, 38, 5, 39, 16, 16, 16, 40, 33, 17, 35, 42, 25, 36, 27, 27, 43, 2]