Python for循环错过了第一次迭代

Python for循环错过了第一次迭代,python,list,Python,List,在上面的代码中,我试图提取外观间隔(由列表中每个数字的第一个和最后一个外观索引分隔),我的方法是解析列表一次,如果该数字仍然不存在于ListoFapearances,则将其附加到第一列,将索引设置为第二列,我将第三列设置为-1 我再次反向解析列表,在列表外观中查找每个元素,如果仍然设置为-1,则相应的第3列将更改为当前索引 这是可行的,但是在向后解析列表时的第一次迭代有一些我无法解决的问题。这个列表示例的结果是: #!/usr/bin/env python new_trace=[1,2,2,3

在上面的代码中,我试图提取外观间隔(由列表中每个数字的第一个和最后一个外观索引分隔),我的方法是解析列表一次,如果该数字仍然不存在于ListoFapearances,则将其附加到第一列,将索引设置为第二列,我将第三列设置为-1

我再次反向解析列表,在列表外观中查找每个元素,如果仍然设置为-1,则相应的第3列将更改为当前索引

这是可行的,但是在向后解析列表时的第一次迭代有一些我无法解决的问题。这个列表示例的结果是:

#!/usr/bin/env python

new_trace=[1,2,2,3,2,1,4,3,2,1,3,4,3,5,6,4,7,6,5,4,5,4,6,6,5,6,4,4,5,6,7,7,6,5,5,7,6,5]

def extractIntervals(new_trace):
    listofAppearances=[[new_trace[0]],[0],[-1]]
    for i in range(0,len(new_trace)-1,1):
        if new_trace[i] in listofAppearances[0]:
            continue
        else:
            listofAppearances[0].append(new_trace[i])
            listofAppearances[1].append(i)
            listofAppearances[2].append(-1)
    print(listofAppearances)

    for j in range(len(new_trace)-1,0,-1):
        for k in range(0,len(listofAppearances[0])-1,1):
            if (new_trace[j]==listofAppearances[0][k]) and (listofAppearances[2][k]==-1):
                listofAppearances[2][k]=j
            else:
                continue

    print(listofAppearances)

def main():
    extractLivenessIntervals(new_trace)

if __name__ == "__main__":
        main()
如您所见,第二个列表的最后一个元素仍然设置为-1,我不明白为什么!我检查了每一寸代码,我不明白为什么会这样

改变一下

[[1, 2, 3, 4, 5, 6, 7], [0, 1, 3, 6, 13, 14, 16], [-1, -1, -1, -1, -1, -1, -1]]
[[1, 2, 3, 4, 5, 6, 7], [0, 1, 3, 6, 13, 14, 16], [9, 8, 12, 27, 37, 36, -1]]

在第17行


编辑:您可以通过以下方式获得相同的结果:

for k in range(0, len(listofAppearances[0]), 1):

我可以建议处理一系列的值吗?首先定义几个辅助函数,然后使用它们将每个元素与它出现的位置分组

def extractIntervals(new_trace):
    listofAppearances = [0, 0, 0]
    listofAppearances[0] = list(set(new_trace))
    # returns new_trace without repeated elements

    listofAppearances[1] = [new_trace.index(i) for i in list(set(new_trace))]
    # returns a list with the index of the first occurrence
    # in new_trace of each element in list(set(new_trace))

    listofAppearances[2] = [len(new_trace) - 1 - new_trace[::-1].index(i) for i in list(set(new_trace))]
    # returns a list with the index of the last occurrence
    # in new_trace of each element in list(set(new_trace))

    print(listofAppearances)
tmp1
是每个元素与它出现的位置列表的配对

def extractIntervals(new_trace):
    listofAppearances = [0, 0, 0]
    listofAppearances[0] = list(set(new_trace))
    # returns new_trace without repeated elements

    listofAppearances[1] = [new_trace.index(i) for i in list(set(new_trace))]
    # returns a list with the index of the first occurrence
    # in new_trace of each element in list(set(new_trace))

    listofAppearances[2] = [len(new_trace) - 1 - new_trace[::-1].index(i) for i in list(set(new_trace))]
    # returns a list with the index of the last occurrence
    # in new_trace of each element in list(set(new_trace))

    print(listofAppearances)
tmp2
是一个三元组列表,由一个列表元素及其出现的第一个和最后一个位置组成

def extractIntervals(new_trace):
    listofAppearances = [0, 0, 0]
    listofAppearances[0] = list(set(new_trace))
    # returns new_trace without repeated elements

    listofAppearances[1] = [new_trace.index(i) for i in list(set(new_trace))]
    # returns a list with the index of the first occurrence
    # in new_trace of each element in list(set(new_trace))

    listofAppearances[2] = [len(new_trace) - 1 - new_trace[::-1].index(i) for i in list(set(new_trace))]
    # returns a list with the index of the last occurrence
    # in new_trace of each element in list(set(new_trace))

    print(listofAppearances)
调用
zip
将三元组列表“解压”为三元组:元素、第一个位置和最后一个位置