简体   繁体   English

如何遍历元组列表,在其中将一个列表中的元组与同一列表中的其他元组进行比较?

[英]How can you loop over lists of tuples where you compare the tuple in one list to the other tuples in the same list?

    for x in check:
        this = sorted(x) #the first tuple
        for y in check:
            that = sorted(y) #the other tuples in the list? in order to compare with 'this'.
            if this == that:
                check.remove(x) 

    print(check)

I basically want to check for every list (in the list 'check') if there are tuples that are the same, such as (1, 3) and (3, 1). 我基本上想检查每个列表(在列表“ check”中)是否存在相同的元组,例如(1、3)和(3、1)。 Then I want to remove the the last one ((3,1)) out of the list 'check'. 然后我想从列表“ check”中删除最后一个((3,1))。 However, the function returns a "list.remove(x): x not in list" error when I use "check.remove(x)". 但是,当我使用“ check.remove(x)”时,该函数返回“ list.remove(x):x不在列表中”错误。 When I used "check.remove(y)", the result was : 当我使用“ check.remove(y)”时,结果是:

output of "check.remove(y)" “ check.remove(y)”的输出

I noticed that the first tuple (of the tuple with the same value) got deleted and that in the second last list, that there is still a pair of tuples that have the same values. 我注意到,第一个元组(具有相同值的元组)已被删除,而在第二个最后一个列表中,仍然存在一对具有相同值的元组。

How the list 'check' looks like 清单“检查”的样子

How can I compare the tuples with each other in the same list and remove the second one that contains the same values? 如何在同一列表中相互比较元组并删除包含相同值的第二个元组?

Repeated removal from a list is never a good a idea since it is O(N) . 反复从列表中删除绝不是一个好主意,因为它是O(N) You can do the cleaning in one non-nested run-through, however. 但是,您可以在一个非嵌套的直通中进行清洁。 It is better to build a clean list from scratch and possibly reassign it to the same variable: 最好从头开始构建一个干净的列表,并可能将其重新分配给相同的变量:

seen, no_dupes = set(), []
for c in check:
    s = tuple(sorted(c))
    if s not in seen:
         seen.add(s)
         no_dupes.append(c)
# check[:] = no_dupes  # if you must

Use in and not == 使用in ,而不是==

for x in check:
    this = sorted(x) #the first tuple
    for y in check:
        that = sorted(y) #the other tuples in the list? in order to compare with 'this'.
        if this in that:
            check.remove(x) 
     # alternatively you might need to loop through this if its a tuple of tuples
     # for t in this:
     #     if t in that:
     #         check.remove(x)

print(check)

Consider the instance [(1,1), (1,1), (1,1)] In the first iteration, x is assigned to the first element in the list, y is also assigned to the first element, since x=y , remove x . 考虑实例[(1,1), (1,1), (1,1)]在第一次迭代中, x分配给列表中的第一个元素, y也分配给第一个元素,因为x=y ,删除x Now when y is iterated to the second element, x=y , but now x has already been removed in the previous iteration. 现在,当y迭代到第二个元素x=y ,但是现在x在上一次迭代中已被删除。 You should use dynamic programming: 您应该使用动态编程:

new_check = []
for x in check:
   this = sorted(x)
   if x not in new_check:
      new_check.append(x)
return new_check

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM