增量重复
Incremental duplicates
我有一个整数列表,列表是循环。我想找到连续的重复项,而不是将重复的数字 + 1 放在起始位置。还需要这样做,直到列表中没有连续的重复项或列表为空。我尝试了不同形式的 while 循环,但无法达到我的目标。尝试用 itertools.cycle
循环,因为 -1 和 0 也是连续的,但无法删除或插入。还找出何时停止是问题,尝试了 int_list == list(set(int_list))
但可能有一些不连续的重复,因此创建了一个无限循环。这是一个例子:
int_list = [1,1,1,2,3,4,5,6,7,7,8,8,9,10,8,11,4,12,15,13,14,14,15,16,1]
processed_list = [11,8,11,4,12,15,13,17]
编辑:
原始问题在 this Codewars 套路中。
deque
接近于循环列表,因为它允许旋转列表。我在下面的代码中利用了这一点。
该算法将 item[0]
与下一项 item[1]
以及前一项(循环意义上的)进行比较 item[-1]
丢弃所有重复项并递增原始项 item[0]
如果发现任何重复项。
如果没有找到重复项,则旋转列表并使用新的 item[0]
重复该过程。
如果没有找到重复的完整列表轮换,则该过程停止。
# Version 3
from collections import deque
def process(inp):
circ = deque(inp)
rcnt = 0 # rotations without finding a dup
while rcnt < len(circ):
n = circ[0]
isdup = False
while len(circ) >= 1 and circ[1] == n:
isdup = True
circ.popleft()
while len(circ) >= 1 and circ[-1] == n:
isdup = True
circ.pop()
if isdup:
circ[0] = n+1
rcnt = 0
else:
circ.rotate(1)
rcnt += 1
# print(list(circ)) # uncomment to watch the progress
return list(circ)
test = [1,1,1,2,3,4,5,6,7,7,8,8,9,10,8,11,4,12,15,13,14,14,15,16,1]
print(process(test))
带有未注释的调试 print
:
[2, 2, 3, 4, 5, 6, 7, 7, 8, 8, 9, 10, 8, 11, 4, 12, 15, 13, 14, 14, 15, 16]
[3, 3, 4, 5, 6, 7, 7, 8, 8, 9, 10, 8, 11, 4, 12, 15, 13, 14, 14, 15, 16]
[4, 4, 5, 6, 7, 7, 8, 8, 9, 10, 8, 11, 4, 12, 15, 13, 14, 14, 15, 16]
[5, 5, 6, 7, 7, 8, 8, 9, 10, 8, 11, 4, 12, 15, 13, 14, 14, 15, 16]
[6, 6, 7, 7, 8, 8, 9, 10, 8, 11, 4, 12, 15, 13, 14, 14, 15, 16]
[7, 7, 7, 8, 8, 9, 10, 8, 11, 4, 12, 15, 13, 14, 14, 15, 16]
[8, 8, 8, 9, 10, 8, 11, 4, 12, 15, 13, 14, 14, 15, 16]
[9, 9, 10, 8, 11, 4, 12, 15, 13, 14, 14, 15, 16]
[10, 10, 8, 11, 4, 12, 15, 13, 14, 14, 15, 16]
[11, 8, 11, 4, 12, 15, 13, 14, 14, 15, 16]
[16, 11, 8, 11, 4, 12, 15, 13, 14, 14, 15]
[15, 16, 11, 8, 11, 4, 12, 15, 13, 14, 14]
[14, 15, 16, 11, 8, 11, 4, 12, 15, 13, 14]
[15, 15, 16, 11, 8, 11, 4, 12, 15, 13]
[16, 16, 11, 8, 11, 4, 12, 15, 13]
[17, 11, 8, 11, 4, 12, 15, 13]
[13, 17, 11, 8, 11, 4, 12, 15]
[15, 13, 17, 11, 8, 11, 4, 12]
[12, 15, 13, 17, 11, 8, 11, 4]
[4, 12, 15, 13, 17, 11, 8, 11]
[11, 4, 12, 15, 13, 17, 11, 8]
[8, 11, 4, 12, 15, 13, 17, 11]
[11, 8, 11, 4, 12, 15, 13, 17]
[17, 11, 8, 11, 4, 12, 15, 13]
[13, 17, 11, 8, 11, 4, 12, 15]
the last line is the result:
[13, 17, 11, 8, 11, 4, 12, 15]
我有一个整数列表,列表是循环。我想找到连续的重复项,而不是将重复的数字 + 1 放在起始位置。还需要这样做,直到列表中没有连续的重复项或列表为空。我尝试了不同形式的 while 循环,但无法达到我的目标。尝试用 itertools.cycle
循环,因为 -1 和 0 也是连续的,但无法删除或插入。还找出何时停止是问题,尝试了 int_list == list(set(int_list))
但可能有一些不连续的重复,因此创建了一个无限循环。这是一个例子:
int_list = [1,1,1,2,3,4,5,6,7,7,8,8,9,10,8,11,4,12,15,13,14,14,15,16,1]
processed_list = [11,8,11,4,12,15,13,17]
编辑:
原始问题在 this Codewars 套路中。
deque
接近于循环列表,因为它允许旋转列表。我在下面的代码中利用了这一点。
该算法将 item[0]
与下一项 item[1]
以及前一项(循环意义上的)进行比较 item[-1]
丢弃所有重复项并递增原始项 item[0]
如果发现任何重复项。
如果没有找到重复项,则旋转列表并使用新的 item[0]
重复该过程。
如果没有找到重复的完整列表轮换,则该过程停止。
# Version 3
from collections import deque
def process(inp):
circ = deque(inp)
rcnt = 0 # rotations without finding a dup
while rcnt < len(circ):
n = circ[0]
isdup = False
while len(circ) >= 1 and circ[1] == n:
isdup = True
circ.popleft()
while len(circ) >= 1 and circ[-1] == n:
isdup = True
circ.pop()
if isdup:
circ[0] = n+1
rcnt = 0
else:
circ.rotate(1)
rcnt += 1
# print(list(circ)) # uncomment to watch the progress
return list(circ)
test = [1,1,1,2,3,4,5,6,7,7,8,8,9,10,8,11,4,12,15,13,14,14,15,16,1]
print(process(test))
带有未注释的调试 print
:
[2, 2, 3, 4, 5, 6, 7, 7, 8, 8, 9, 10, 8, 11, 4, 12, 15, 13, 14, 14, 15, 16]
[3, 3, 4, 5, 6, 7, 7, 8, 8, 9, 10, 8, 11, 4, 12, 15, 13, 14, 14, 15, 16]
[4, 4, 5, 6, 7, 7, 8, 8, 9, 10, 8, 11, 4, 12, 15, 13, 14, 14, 15, 16]
[5, 5, 6, 7, 7, 8, 8, 9, 10, 8, 11, 4, 12, 15, 13, 14, 14, 15, 16]
[6, 6, 7, 7, 8, 8, 9, 10, 8, 11, 4, 12, 15, 13, 14, 14, 15, 16]
[7, 7, 7, 8, 8, 9, 10, 8, 11, 4, 12, 15, 13, 14, 14, 15, 16]
[8, 8, 8, 9, 10, 8, 11, 4, 12, 15, 13, 14, 14, 15, 16]
[9, 9, 10, 8, 11, 4, 12, 15, 13, 14, 14, 15, 16]
[10, 10, 8, 11, 4, 12, 15, 13, 14, 14, 15, 16]
[11, 8, 11, 4, 12, 15, 13, 14, 14, 15, 16]
[16, 11, 8, 11, 4, 12, 15, 13, 14, 14, 15]
[15, 16, 11, 8, 11, 4, 12, 15, 13, 14, 14]
[14, 15, 16, 11, 8, 11, 4, 12, 15, 13, 14]
[15, 15, 16, 11, 8, 11, 4, 12, 15, 13]
[16, 16, 11, 8, 11, 4, 12, 15, 13]
[17, 11, 8, 11, 4, 12, 15, 13]
[13, 17, 11, 8, 11, 4, 12, 15]
[15, 13, 17, 11, 8, 11, 4, 12]
[12, 15, 13, 17, 11, 8, 11, 4]
[4, 12, 15, 13, 17, 11, 8, 11]
[11, 4, 12, 15, 13, 17, 11, 8]
[8, 11, 4, 12, 15, 13, 17, 11]
[11, 8, 11, 4, 12, 15, 13, 17]
[17, 11, 8, 11, 4, 12, 15, 13]
[13, 17, 11, 8, 11, 4, 12, 15]
the last line is the result:
[13, 17, 11, 8, 11, 4, 12, 15]