Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
List Python-比较列表中的元组_List_Python 3.x_Tuples - Fatal编程技术网

List Python-比较列表中的元组

List Python-比较列表中的元组,list,python-3.x,tuples,List,Python 3.x,Tuples,在我创建的程序中,我有一个包含元组的列表,每个元组包含3个数字。例如 my_list = [(1, 2, 4), (2, 4, 1), (1, 5, 2), (1, 4, 1),...] 现在我想删除最后两个数字小于其他任何元组最后两个数字的任何元组 要删除元组,第一个数字必须相同* 所以在上面的元组列表中,这会发生 my_list = [(1, 2, 4), (2, 4, 1), (1, 5, 2), (1, 4, 1),...] # some code... result = [(1,

在我创建的程序中,我有一个包含元组的列表,每个元组包含3个数字。例如

my_list = [(1, 2, 4), (2, 4, 1), (1, 5, 2), (1, 4, 1),...]
现在我想删除最后两个数字小于其他任何元组最后两个数字的任何元组

  • 要删除元组,第一个数字必须相同*
所以在上面的元组列表中,这会发生

my_list = [(1, 2, 4), (2, 4, 1), (1, 5, 2), (1, 4, 1),...]
# some code...
result = [(1, 2, 4), (2, 4, 1), (1, 5, 2)]
第一个元组不会被删除,因为(2和4)不小于(4和1->2<4但4>1),(1和5->2>1)或(4和1->2<4但4>1)

第二个元组不会被删除,因为它的第一个数字(2)与其他每个元组的第一个数字不同

不删除第三个元组的原因与不删除第一个元组的原因相同

第四个元组被删除,因为(4和1)小于(5和2->4<5和1<2)


我真的需要帮助,因为我陷入了我的计划,我不知道该怎么办。我不是在要求一个解决方案,而是关于如何开始解决这个问题的一些指导。非常感谢你

我想这可能真的管用。我刚想出来。这是最好的解决方案吗

results = [(1, 2, 4), (2, 4, 1), (1, 5, 2), (1, 4, 1)]
for position in results:
    for check in results:
        if position[0] == check[0] and position[1] < check[1] and position[2] < check[2]:
            results.remove(position)
results=[(1,2,4)、(2,4,1)、(1,5,2)、(1,4,1)]
对于结果中的位置:
有关签入结果:
如果位置[0]==检查[0]和位置[1]<检查[1]和位置[2]<检查[2]:
结果:移除(位置)

执行此操作的简单列表理解:

[i for i in l if not any([i[0]==j[0] and i[1]<j[1] and i[2]<j[2] for j in my_list])]

[i for i in l if not any([i[0]==j[0]和i[1]在迭代结构时永远不会修改它们。在副本上迭代。理论上,除了Coldspeed提到的内容外,这是可行的。在迭代过程中永远不要修改迭代器。
my_list = [(1, 2, 4), (2, 4, 1), (1, 5, 2), (1, 4, 1)]
results = []
for position in my_list:
    for check in my_list:
        if not (position[0] == check[0] and position[1] < check[1] and position[2] < check[2]):
            results.append(position)

results
>[(1, 2, 4), (2, 4, 1), (1, 5, 2)]