在数组中查找和输出一行重复整数的简单方法
例如,1 2 3 4 4 4 5 6
数组应输出4 4 4
答案 0 :(得分:0)
对于角落情况,有很多方法可以用不同的输出来回答这个问题,但对于一个能够正确解决具体提到的案例的最小例子,这有多少?
x = [1, 2, 3, 4, 4, 4, 5, 6]
ind = [a==b for a,b in zip(x[:-1],x[1:])].index(True)
y = [a for a in x if a==x[ind]]
print(y)
给出[4, 4, 4]
答案 1 :(得分:0)
下面的代码将列出所有重复项目的序列表。
list = [1, 2, 3, 4, 4, 4, 5, 6, 6, 7, 8, 8, 8, 8, 3, 4, 2, 3, 3]
last = None
sequences = []
series = []
list.append('last')
for item in list:
if item == last:
series.append(last)
elif series:
series.append(last)
sequences.append(series)
series = []
last = item
if series:
series.append(last)
sequences.append(series)
print sequences
将输出
>>> [[4, 4, 4], [6, 6], [8, 8, 8, 8], [3, 3]]