x = [1, 2, 2, 3, 4, 6, 6]
result = []
cur_idx = 0
i = 1
while i < len(x):
if x[cur_idx] == x[i]:
result.append([cur_idx, x[cur_idx]])
# Increase value of i up to the index where there is different value
while i < len(x):
if x[cur_idx] != x[i]:
# Set cur_idx to the index of different value
cur_idx = i
i += 1
break
i += 1
else:
cur_idx = i
i += 1
print(result)
# [[1, 3], [5, 3]]
def repeating_values( initital_list):
cache = {}
result = []
for i, element in enumerate(initital_list):
if element in cache:
result.append([cache[initital_list[i]], i])
else:
cache[element] = i
return result
# Hello World program in Python
x = [1,2,2,3,4,6,6]
new_list = []
i = 0
indexes = []
while i < len(x)-1:
if x[i] == x[i+1]:
indexes.append(i)
indexes.append(i+1)
else:
indexes = list(set(indexes))
if len(indexes) != 0:
new_list.append(indexes)
indexes = []
i+=1
indexes = list(set(indexes))
if len(indexes) != 0:
new_list.append(indexes)
print(new_list)
可以跟踪当前索引,当下一个元素等于当前_索引中的值时,可以将其附加到结果中,但需要增加索引的值,直到其值不同为止
试试这个
谢谢你的意见
结果
请尝试以下代码:
结果:
相关问题 更多 >
编程相关推荐