遍历列表并比较元素

时间:2017-11-06 19:54:36

标签: python-3.x

我正在遍历二进制元素列表。当我到达第四个及后续元素时,会发生什么取决于之前列表中的内容。

请查看我的代码,看看你是否可以帮我弄清楚它为什么没有查看正确的索引。我的程序占用了列表中的第四个元素。你是所有独特子集的统计者。有时,u中的元素必须用于计算附加到u的下一个项目。

我是所有这一切的新手,非常感谢任何帮助。

#Computer will read each element after the third element, one at a time, and 
#evaluate the element itself and how it relates to the previous elements.
#if next element is same as the element before, then computer returns the 
#same number to the list as the calculation before.
#If the next element is not the same as the element before, then computer 
#returns the sum of the last two calculations entered into the list.
#If the next element is not the same as the element before, but is coming 
#off of two same elements, then computer returns the sum of the last three 
#calculations in the list into the list.
#If element is a new element, then computer returns a 1 to the list.

if index > 2:
    print ("These are the next terms.")

    r = int (u[-1])
    s = int (u[-2])
    t = int (u[-3])
    w = [r,s]
    q = [r,s,t]

    print ("This element is: ", element)
    if element in L[:-1]: #if seen before
        print ("this element has been seen before")
        if element == L[-2]: #same as previous element
            print("this element is the same as the previous element")
            u.append(u[-1])
            print(u)
        else: #not same as previous
            if L[-2] != L[-3]:
                print("this element is NOT the same as the previous element")
                u.append(sum(w))
                print(u)
            else:
                if element != L[-2]:# not same, happened before, off of 2 identicals
                    if element in L[:-1]:
                        if L[-2]==L[-3]:
                            print("this element is not the same, coming off two identicals")
                            u.append(sum(q))
                            print (sum(q))
                            print (u)             
    else:
        if element != L[:]: #not the same as previous, but NEW
            print("this element is NEW!")
            u.append(sum(q) + 1)
            print (u)

0 个答案:

没有答案
相关问题