Python 如何将基于最后一个元素的元组列表与条件进行比较

Python 如何将基于最后一个元素的元组列表与条件进行比较,python,list,tuples,Python,List,Tuples,我试图将当前列表中每个元组的最后一个元素中的元组列表与一些条件进行比较 我有这样的清单: index = [ ([('T', (0, 0)), ('T', (1, 0)), ('T', (2, 0))], (1, 0)), ([('T', (0, 1)), ('M', (1, 1)), ('T', (2, 1))], (1, 1)), ([('T', (0, 2)), ('M', (1, 2)), ('T', (2, 2))], (1, 1)),

我试图将当前列表中每个元组的最后一个元素中的元组列表与一些条件进行比较

我有这样的清单:

index = [
        ([('T', (0, 0)), ('T', (1, 0)), ('T', (2, 0))], (1, 0)),
        ([('T', (0, 1)), ('M', (1, 1)), ('T', (2, 1))], (1, 1)),
        ([('T', (0, 2)), ('M', (1, 2)), ('T', (2, 2))], (1, 1)),
        ([('T', (0, 3)), ('M', (1, 3)), ('T', (2, 3))], (1, 1)),
        ([('T', (0, 4)), ('T', (1, 4)), ('T', (2, 4))], (1, 0))
        ]
预期产出为:

[
    [
        [('T', (0, 0)), ('T', (1, 0)), ('T', (2, 0))],
        [('T', (0, 1)), ('M', (1, 1)), ('T', (2, 1))]
    ],
    [
        [('T', (0, 2)), ('M', (1, 2)), ('T', (2, 2))]
    ],
    [
        [('T', (0, 3)), ('M', (1, 3)), ('T', (2, 3))], 
        [('T', (0, 4)), ('T', (1, 4)), ('T', (2, 4))]
    ]
]
def get_final(index, step = 2):
    k, i = 0, 0
    f_m, f_s = [], []
    while k < len(index):
        while i <= step:
            cond1 = (index[k+i][1][0] == 1 and index[k+i][1][1] == 0) and index[k+i+1][1][1] == 1
            cond2 = (index[k+i][1][0] == 0 and index[k+i][1][1] == 1) and index[k+i+1][1][0] == 0
            cond3 = (index[k+i][1][0] == 1 and index[k+i][1][1] == 1) and index[k+i+1][1][0] == 0

            if cond1:
                f_m += [index[k+i][0], index[k+i+1][0]]
                i += 2

            elif cond2:
                f_m += [index[k+i][0], index[k+i+1][0]]
                i += 2

            elif cond3:
                f_m += [index[k+i][0], index[k+i+1][0]]
                i += 2
            elif not cond1 and not cond2 and not cond3:
                f_s += index[k+i][0]
                i+=1

        k+=i

    print("f_m: ", f_m)
    print("f_s: ", f_s)
f_m:  [
        [('T', (0, 0)), ('T', (1, 0)), ('T', (2, 0))],
        [('T', (0, 1)), ('M', (1, 1)), ('T', (2, 1))]
      ]
f_s:  [('T', (0, 2)), ('M', (1, 2)), ('T', (2, 2))]
我要做的是比较元组的最后一个元素并应用一些条件

条件是:

Compare Position k and k+1:
    1- if i found ((1,0) and (1,1)) or ((0,1) and (1,0)) 
       or ((1,1) and (1,0)) or ((1,1) and (0,1)) 
       => append to a list called `f_m` and go to `Position k+2`
    2- if i found (1,1) and (1,1) 
       => append to a list called `f_s` and go to `Position k+1`
0类似的东西

index = [
        ([('T', (0, 0)), ('T', (1, 0)), ('T', (2, 0))], (1, 0)),
        ([('T', (0, 1)), ('M', (1, 1)), ('T', (2, 1))], (1, 1)),
        ([('T', (0, 2)), ('M', (1, 2)), ('T', (2, 2))], (1, 1)),
        ([('T', (0, 3)), ('M', (1, 3)), ('T', (2, 3))], (1, 1)),
        ([('T', (0, 4)), ('T', (1, 4)), ('T', (2, 4))], (1, 0))
        ]

newlist = []
i=0
while i<len(index):
    if index[i][1]+index[i+1][1] in [(1,0,1,1), (0,1,1,0), (1,1,1,0), (1,1,0,1)]:
        newlist.append([index[i][0]] + [index[i+1][0]])
        i+=2
    elif index[i][1]+index[i+1][1] == (1,1,1,1):
        newlist.append([index[i][0]])
        i+=1

print newlist

谢谢它确实输出了我所期望的。但是如果更改
步骤
?例如,使用
step=4
?@nexus66立即检查。根据您的要求调整函数内的步长值。使用
step=4
打印
索引器。该步骤用于同时比较具有相同逻辑的示例4元组。也许这是另一个问题。否则我会接受你的回答谢谢。顺便说一句,谢谢你提示我将元组与这个列表进行比较
[(1,0,1,1,1,0),(1,1,1,0),(1,1,0,1)]
。我想我可以做剩下的。非常感谢。你能解释一下这个问题出了什么问题吗?我认为步长在这里没有意义。因为如果更改步长,则
i=i+step
的条件也会更改。所以您还需要传递该参数,它不是动态的。@MYGz谢谢您的评论。我会设法弄明白的。谢谢你的回答和提示。
[
     [
        [('T', (0, 0)), ('T', (1, 0)), ('T', (2, 0))],
        [('T', (0, 1)), ('M', (1, 1)), ('T', (2, 1))]
     ],

     [
        [('T', (0, 2)), ('M', (1, 2)), ('T', (2, 2))]
     ],

     [
        [('T', (0, 3)), ('M', (1, 3)), ('T', (2, 3))],
        [('T', (0, 4)), ('T', (1, 4)), ('T', (2, 4))]
     ]
 ]