Python 查找列表中连续项的索引

Python 查找列表中连续项的索引,python,python-3.x,list,indexing,while-loop,Python,Python 3.x,List,Indexing,While Loop,我想列一个如下的清单: x = [1,2,2,3,4,6,6] 并返回一个二维列表,其中包含重复值的索引: [ [1,2], [5,6]] 我尝试了以下方法: new_list = [] i = 0 while i < len(x)-1: if x[i] == x[i+1]: new_list.append([x[i],x[i+1]] i += 1 试试这个 def repeating_values( initital_list): cache

我想列一个如下的清单:

x = [1,2,2,3,4,6,6]
并返回一个二维列表,其中包含重复值的索引:

[ [1,2], [5,6]]
我尝试了以下方法:

new_list = []
i = 0
while i < len(x)-1:
    if x[i] == x[i+1]:
        new_list.append([x[i],x[i+1]]
    i += 1
试试这个

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 

谢谢你的意见


x = [1,2,2,3,4,6,6]

print(repeating_values(x))
结果

[[1, 2], [5, 6]]
试试这个

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 

谢谢你的意见


x = [1,2,2,3,4,6,6]

print(repeating_values(x))
结果

[[1, 2], [5, 6]]

可以跟踪当前索引,当下一个元素等于当前_索引中的值时,可以将其附加到结果中,但需要增加索引的值,直到其值不同为止

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]]
x=[1,2,2,3,4,6,6]
结果=[]
cur_idx=0
i=1
而i
您可以跟踪当前索引,当下一个元素等于当前索引中的值时,您可以将其附加到结果中,但需要增加索引的值,直到其值不同为止

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]]
x=[1,2,2,3,4,6,6]
结果=[]
cur_idx=0
i=1
而i
尝试以下代码:

# 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)
请尝试以下代码:

# 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)
你可以在这里用

纯Python方法:

x=[1, 2, 2, 2, 3, 3, 4, 4, 6, 6, 6, 6, 6, 6, 8]
out=[]
count=0
for i in range(1,len(x)):
    prev=x[i-1]
    if x[i]==prev:
        count+=1
    elif count and x[i]!=prev:
        out.append([i-count-1,i-1])
        count=0
if count:
    out.append([i-count,i])

out
# [[1, 3], [4, 5], [6, 7], [8, 13]]
你可以在这里用

纯Python方法:

x=[1, 2, 2, 2, 3, 3, 4, 4, 6, 6, 6, 6, 6, 6, 8]
out=[]
count=0
for i in range(1,len(x)):
    prev=x[i-1]
    if x[i]==prev:
        count+=1
    elif count and x[i]!=prev:
        out.append([i-count-1,i-1])
        count=0
if count:
    out.append([i-count,i])

out
# [[1, 3], [4, 5], [6, 7], [8, 13]]

初始列表是否已排序?能否提供有关数组
x
的更多详细信息?如果x=[1,1,1,2,2],那么所需的输出是[[0,2],[3,5]?是的,这将是所需的输出如果重复的值不按顺序排列,则如何处理,例如:
x=[1,2,1,2]
在这种情况下的答案是
[[0,2],[1,3]]
初始列表是否已排序?能否提供有关数组的更多详细信息
x
?如果x=[1,1,1,2,2,2],那么所需的输出是[[0,2],[3,5]?是的,这将是所需的输出如果重复的值不是按顺序排列的,那么如何处理,例如:
x=[1,2,1,2]
这种情况下的答案是
[[0,2],[1,3]