Python 为什么我不能删除列表中的某些值? 匹配的_边=[0,-1,1,-1,2,3,3,2,4,6,5,7,6,4,7,5,8,9,9,8] 过程=匹配的_边 印刷工艺 对于匹配_边中的tup: 如果tup[1],则进程中的tup[0]: process.removetup[1],tup[0] 如果tup中为-1: 清除 打印图 印刷工艺

Python 为什么我不能删除列表中的某些值? 匹配的_边=[0,-1,1,-1,2,3,3,2,4,6,5,7,6,4,7,5,8,9,9,8] 过程=匹配的_边 印刷工艺 对于匹配_边中的tup: 如果tup[1],则进程中的tup[0]: process.removetup[1],tup[0] 如果tup中为-1: 清除 打印图 印刷工艺,python,Python,[0,-1,1,-1,2,3,3,2,4,6,5,7,6,4,7,5,8,9,9,8] 0, -1 [1,-1,2,3,4,6,5,7,8,9] 我尝试了很多方法,但我就是不能删除1,-1。怎么了 -1:1,-1 真的 你试过类似的东西吗 process = [tup for tup in matched_edges if (tup[0] != -1 and tup[1] != -1)] 这将删除任何包含-1的元组 编辑 另外,代码行为异常的原因是匹配的_边和进程都持有相同的对象,所以当您从进

[0,-1,1,-1,2,3,3,2,4,6,5,7,6,4,7,5,8,9,9,8] 0, -1 [1,-1,2,3,4,6,5,7,8,9]

我尝试了很多方法,但我就是不能删除1,-1。怎么了

-1:1,-1 真的


你试过类似的东西吗

process = [tup for tup in matched_edges if (tup[0] != -1 and tup[1] != -1)]
这将删除任何包含-1的元组

编辑

另外,代码行为异常的原因是匹配的_边和进程都持有相同的对象,所以当您从进程中移除对象时,您也在迭代过程中从匹配的_边中移除它们,从而导致意外的行为。运行下面的代码,你就会明白我的意思

matched_edges = [(0, -1), (1, -1), (2, 3), (3, 2), (4, 6), (5, 7), (6, 4), (7, 5), (8, 9), (9, 8)]
process = matched_edges
print(process)

for tup in matched_edges:
    print(tup)
    print("Length of matched edges:", len(matched_edges))
    if (tup[1], tup[0]) in process:
        process.remove((tup[1], tup[0]))
    if -1 in tup:
        process.remove(tup)
        print("removing", tup)
print(process)

matched_edges = [tup for tup in matched_edges if (tup[0] != -1 and tup[1] != -1)]
print(matched_edges)

你试过类似的东西吗

process = [tup for tup in matched_edges if (tup[0] != -1 and tup[1] != -1)]
这将删除任何包含-1的元组

编辑

另外,代码行为异常的原因是匹配的_边和进程都持有相同的对象,所以当您从进程中移除对象时,您也在迭代过程中从匹配的_边中移除它们,从而导致意外的行为。运行下面的代码,你就会明白我的意思

matched_edges = [(0, -1), (1, -1), (2, 3), (3, 2), (4, 6), (5, 7), (6, 4), (7, 5), (8, 9), (9, 8)]
process = matched_edges
print(process)

for tup in matched_edges:
    print(tup)
    print("Length of matched edges:", len(matched_edges))
    if (tup[1], tup[0]) in process:
        process.remove((tup[1], tup[0]))
    if -1 in tup:
        process.remove(tup)
        print("removing", tup)
print(process)

matched_edges = [tup for tup in matched_edges if (tup[0] != -1 and tup[1] != -1)]
print(matched_edges)

您可以创建一个新列表

matched_edges = [(0, -1), (1, -1), (2, 3), (3, 2), (4, 6), (5, 7), (6, 4), (7, 5), (8, 9), (9, 8)]
process = []
for tup in matched_edges:
    if (tup[1], tup[0]) in matched_edges:matched_edges.remove((tup[1], tup[0]))
    if -1 not in tup:process.append(tup)
print(process)
输出


您可以创建一个新列表

matched_edges = [(0, -1), (1, -1), (2, 3), (3, 2), (4, 6), (5, 7), (6, 4), (7, 5), (8, 9), (9, 8)]
process = []
for tup in matched_edges:
    if (tup[1], tup[0]) in matched_edges:matched_edges.remove((tup[1], tup[0]))
    if -1 not in tup:process.append(tup)
print(process)
输出


这是错误的线路

process = matched_edges
这是一条编程规则。尽可能不要更新当前正在迭代的对象!代码中所做的是,进程和匹配的_边指向/引用堆内存中相同的对象/列表不同的堆栈变量,但都指向堆中相同的内存地址,即实际的列表。因此,更新进程意味着您也在间接更新匹配的_边

您当前正在迭代匹配的_边:

for tup in matched_edges:
process.remove(...)
然后,您更新了流程,这会打乱匹配_边的迭代:

for tup in matched_edges:
process.remove(...)
对代码的更正是使用以下内容:

process = matched_edges.copy()

这是错误的线路

process = matched_edges
这是一条编程规则。尽可能不要更新当前正在迭代的对象!代码中所做的是,进程和匹配的_边指向/引用堆内存中相同的对象/列表不同的堆栈变量,但都指向堆中相同的内存地址,即实际的列表。因此,更新进程意味着您也在间接更新匹配的_边

您当前正在迭代匹配的_边:

for tup in matched_edges:
process.remove(...)
然后,您更新了流程,这会打乱匹配_边的迭代:

for tup in matched_edges:
process.remove(...)
对代码的更正是使用以下内容:

process = matched_edges.copy()
此行过程=匹配的_边实际上是创建对匹配的_边列表的引用,而不是项目的副本。反过来,这会影响for循环中的迭代器,因为您要从进程和匹配的_边中删除项。为了演示,我稍微修改了您的代码,只是为了打印值:

matched_edges = [(0, -1), (1, -1), (2, 3), (3, 2), (4, 6), (5, 7), (6, 4), (7, 5), (8, 9), (9, 8)]
process = matched_edges

for tup in matched_edges:
    print(f"Current tup: {tup}")
    print(f"Process: {process}")
    print(f"Matched: {matched_edges}")
    if (tup[1], tup[0]) in process:
        process.remove((tup[1], tup[0]))
    if -1 in tup:
        process.remove(tup)
输出:

Current tup: (0, -1)
Process: [(0, -1), (1, -1), (2, 3), (3, 2), (4, 6), (5, 7), (6, 4), (7, 5), (8, 9), (9, 8)]
Matched: [(0, -1), (1, -1), (2, 3), (3, 2), (4, 6), (5, 7), (6, 4), (7, 5), (8, 9), (9, 8)]
Current tup: (2, 3)
Process: [(1, -1), (2, 3), (3, 2), (4, 6), (5, 7), (6, 4), (7, 5), (8, 9), (9, 8)]
Matched: [(1, -1), (2, 3), (3, 2), (4, 6), (5, 7), (6, 4), (7, 5), (8, 9), (9, 8)]
Current tup: (4, 6)
Process: [(1, -1), (2, 3), (4, 6), (5, 7), (6, 4), (7, 5), (8, 9), (9, 8)]
Matched: [(1, -1), (2, 3), (4, 6), (5, 7), (6, 4), (7, 5), (8, 9), (9, 8)]
Current tup: (5, 7)
Process: [(1, -1), (2, 3), (4, 6), (5, 7), (7, 5), (8, 9), (9, 8)]
Matched: [(1, -1), (2, 3), (4, 6), (5, 7), (7, 5), (8, 9), (9, 8)]
Current tup: (8, 9)
Process: [(1, -1), (2, 3), (4, 6), (5, 7), (8, 9), (9, 8)]
Matched: [(1, -1), (2, 3), (4, 6), (5, 7), (8, 9), (9, 8)]
您可以看到,在第二次迭代中,您最终跳过了1,-1元组。通常,每次删除项目时都会跳过该项目

将作业更改为“处理”以使用“复制”,以便获得新列表:

process = matched_edges.copy()
这应该可以纠正这种奇怪的行为。虽然我不太确定您使用的循环是否仍然完全符合您的预期,但这是复制列表的输出:

Current tup: (0, -1)
Process: [(0, -1), (1, -1), (2, 3), (3, 2), (4, 6), (5, 7), (6, 4), (7, 5), (8, 9), (9, 8)]
Matched: [(0, -1), (1, -1), (2, 3), (3, 2), (4, 6), (5, 7), (6, 4), (7, 5), (8, 9), (9, 8)]
Current tup: (1, -1)
Process: [(1, -1), (2, 3), (3, 2), (4, 6), (5, 7), (6, 4), (7, 5), (8, 9), (9, 8)]
Matched: [(0, -1), (1, -1), (2, 3), (3, 2), (4, 6), (5, 7), (6, 4), (7, 5), (8, 9), (9, 8)]
Current tup: (2, 3)
Process: [(2, 3), (3, 2), (4, 6), (5, 7), (6, 4), (7, 5), (8, 9), (9, 8)]
Matched: [(0, -1), (1, -1), (2, 3), (3, 2), (4, 6), (5, 7), (6, 4), (7, 5), (8, 9), (9, 8)]
Current tup: (3, 2)
Process: [(2, 3), (4, 6), (5, 7), (6, 4), (7, 5), (8, 9), (9, 8)]
Matched: [(0, -1), (1, -1), (2, 3), (3, 2), (4, 6), (5, 7), (6, 4), (7, 5), (8, 9), (9, 8)]
Current tup: (4, 6)
Process: [(4, 6), (5, 7), (6, 4), (7, 5), (8, 9), (9, 8)]
Matched: [(0, -1), (1, -1), (2, 3), (3, 2), (4, 6), (5, 7), (6, 4), (7, 5), (8, 9), (9, 8)]
Current tup: (5, 7)
Process: [(4, 6), (5, 7), (7, 5), (8, 9), (9, 8)]
Matched: [(0, -1), (1, -1), (2, 3), (3, 2), (4, 6), (5, 7), (6, 4), (7, 5), (8, 9), (9, 8)]
Current tup: (6, 4)
Process: [(4, 6), (5, 7), (8, 9), (9, 8)]
Matched: [(0, -1), (1, -1), (2, 3), (3, 2), (4, 6), (5, 7), (6, 4), (7, 5), (8, 9), (9, 8)]
Current tup: (7, 5)
Process: [(5, 7), (8, 9), (9, 8)]
Matched: [(0, -1), (1, -1), (2, 3), (3, 2), (4, 6), (5, 7), (6, 4), (7, 5), (8, 9), (9, 8)]
Current tup: (8, 9)
Process: [(8, 9), (9, 8)]
Matched: [(0, -1), (1, -1), (2, 3), (3, 2), (4, 6), (5, 7), (6, 4), (7, 5), (8, 9), (9, 8)]
Current tup: (9, 8)
Process: [(8, 9)]
Matched: [(0, -1), (1, -1), (2, 3), (3, 2), (4, 6), (5, 7), (6, 4), (7, 5), (8, 9), (9, 8)]
此行过程=匹配的_边实际上是创建对匹配的_边列表的引用,而不是项目的副本。反过来,这会影响for循环中的迭代器,因为您要从进程和匹配的_边中删除项。为了演示,我稍微修改了您的代码,只是为了打印值:

matched_edges = [(0, -1), (1, -1), (2, 3), (3, 2), (4, 6), (5, 7), (6, 4), (7, 5), (8, 9), (9, 8)]
process = matched_edges

for tup in matched_edges:
    print(f"Current tup: {tup}")
    print(f"Process: {process}")
    print(f"Matched: {matched_edges}")
    if (tup[1], tup[0]) in process:
        process.remove((tup[1], tup[0]))
    if -1 in tup:
        process.remove(tup)
输出:

Current tup: (0, -1)
Process: [(0, -1), (1, -1), (2, 3), (3, 2), (4, 6), (5, 7), (6, 4), (7, 5), (8, 9), (9, 8)]
Matched: [(0, -1), (1, -1), (2, 3), (3, 2), (4, 6), (5, 7), (6, 4), (7, 5), (8, 9), (9, 8)]
Current tup: (2, 3)
Process: [(1, -1), (2, 3), (3, 2), (4, 6), (5, 7), (6, 4), (7, 5), (8, 9), (9, 8)]
Matched: [(1, -1), (2, 3), (3, 2), (4, 6), (5, 7), (6, 4), (7, 5), (8, 9), (9, 8)]
Current tup: (4, 6)
Process: [(1, -1), (2, 3), (4, 6), (5, 7), (6, 4), (7, 5), (8, 9), (9, 8)]
Matched: [(1, -1), (2, 3), (4, 6), (5, 7), (6, 4), (7, 5), (8, 9), (9, 8)]
Current tup: (5, 7)
Process: [(1, -1), (2, 3), (4, 6), (5, 7), (7, 5), (8, 9), (9, 8)]
Matched: [(1, -1), (2, 3), (4, 6), (5, 7), (7, 5), (8, 9), (9, 8)]
Current tup: (8, 9)
Process: [(1, -1), (2, 3), (4, 6), (5, 7), (8, 9), (9, 8)]
Matched: [(1, -1), (2, 3), (4, 6), (5, 7), (8, 9), (9, 8)]
您可以看到,在第二次迭代中,您最终跳过了1,-1元组。通常,每次删除项目时都会跳过该项目

将作业更改为“处理”以使用“复制”,以便获得新列表:

process = matched_edges.copy()
这应该可以纠正这种奇怪的行为。虽然我不太确定您使用的循环是否仍然完全符合您的预期,但这是复制列表的输出:

Current tup: (0, -1)
Process: [(0, -1), (1, -1), (2, 3), (3, 2), (4, 6), (5, 7), (6, 4), (7, 5), (8, 9), (9, 8)]
Matched: [(0, -1), (1, -1), (2, 3), (3, 2), (4, 6), (5, 7), (6, 4), (7, 5), (8, 9), (9, 8)]
Current tup: (1, -1)
Process: [(1, -1), (2, 3), (3, 2), (4, 6), (5, 7), (6, 4), (7, 5), (8, 9), (9, 8)]
Matched: [(0, -1), (1, -1), (2, 3), (3, 2), (4, 6), (5, 7), (6, 4), (7, 5), (8, 9), (9, 8)]
Current tup: (2, 3)
Process: [(2, 3), (3, 2), (4, 6), (5, 7), (6, 4), (7, 5), (8, 9), (9, 8)]
Matched: [(0, -1), (1, -1), (2, 3), (3, 2), (4, 6), (5, 7), (6, 4), (7, 5), (8, 9), (9, 8)]
Current tup: (3, 2)
Process: [(2, 3), (4, 6), (5, 7), (6, 4), (7, 5), (8, 9), (9, 8)]
Matched: [(0, -1), (1, -1), (2, 3), (3, 2), (4, 6), (5, 7), (6, 4), (7, 5), (8, 9), (9, 8)]
Current tup: (4, 6)
Process: [(4, 6), (5, 7), (6, 4), (7, 5), (8, 9), (9, 8)]
Matched: [(0, -1), (1, -1), (2, 3), (3, 2), (4, 6), (5, 7), (6, 4), (7, 5), (8, 9), (9, 8)]
Current tup: (5, 7)
Process: [(4, 6), (5, 7), (7, 5), (8, 9), (9, 8)]
Matched: [(0, -1), (1, -1), (2, 3), (3, 2), (4, 6), (5, 7), (6, 4), (7, 5), (8, 9), (9, 8)]
Current tup: (6, 4)
Process: [(4, 6), (5, 7), (8, 9), (9, 8)]
Matched: [(0, -1), (1, -1), (2, 3), (3, 2), (4, 6), (5, 7), (6, 4), (7, 5), (8, 9), (9, 8)]
Current tup: (7, 5)
Process: [(5, 7), (8, 9), (9, 8)]
Matched: [(0, -1), (1, -1), (2, 3), (3, 2), (4, 6), (5, 7), (6, 4), (7, 5), (8, 9), (9, 8)]
Current tup: (8, 9)
Process: [(8, 9), (9, 8)]
Matched: [(0, -1), (1, -1), (2, 3), (3, 2), (4, 6), (5, 7), (6, 4), (7, 5), (8, 9), (9, 8)]
Current tup: (9, 8)
Process: [(8, 9)]
Matched: [(0, -1), (1, -1), (2, 3), (3, 2), (4, 6), (5, 7), (6, 4), (7, 5), (8, 9), (9, 8)]

问题是,如果您执行process=matched_edges,那么您正在创建一个变量流程,该流程指向与matched_edges相同的对象,以创建一个具有相同值的新列表,您应该改为执行process=matched_edges[:]

因此,您的代码如下所示:

matched_edges = [(0, -1), (1, -1), (2, 3), (3, 2), (4, 6), (5, 7), (6, 4), (7, 5), (8, 9), (9, 8)]
process = matched_edges[:]
print(process)

for tup in matched_edges:
    if (tup[1], tup[0]) in process:
        process.remove((tup[1], tup[0]))
    if -1 in tup:
        process.remove(tup)
        print(tup)
print(process)

问题是,如果您执行process=matched_edges,那么您正在创建一个变量流程,该流程指向与matched_edges相同的对象,以创建一个具有相同值的新列表,您应该改为执行process=matched_edges[:]

因此,您的代码如下所示:

matched_edges = [(0, -1), (1, -1), (2, 3), (3, 2), (4, 6), (5, 7), (6, 4), (7, 5), (8, 9), (9, 8)]
process = matched_edges[:]
print(process)

for tup in matched_edges:
    if (tup[1], tup[0]) in process:
        process.remove((tup[1], tup[0]))
    if -1 in tup:
        process.remove(tup)
        print(tup)
print(process)

以下内容对我来说很好

matched_edges = [(0, -1), (1, -1), (2, 3), (3, 2), (4, 6), 
                (5, 7), (6, 4), (7, 5), (8, 9), (9, 8)]

process = []

for tup in matched_edges:
    if (tup[0] != -1 and tup[1] != -1) and (tup[1], tup[0]) not in process:
        process.append(tup)
print(process)
输出

[(2, 3), (4, 6), (5, 7), (8, 9)]

以下内容对我来说很好

matched_edges = [(0, -1), (1, -1), (2, 3), (3, 2), (4, 6), 
                (5, 7), (6, 4), (7, 5), (8, 9), (9, 8)]

process = []

for tup in matched_edges:
    if (tup[0] != -1 and tup[1] != -1) and (tup[1], tup[0]) not in process:
        process.append(tup)
print(process)
输出

[(2, 3), (4, 6), (5, 7), (8, 9)]

移除元组必须满足什么条件?当元组中的-1或顺序相同但不同的元组时。如1,2和2,1,则我需要移除其中一个。这是否回答了您的问题?
请记住,过程和匹配的_边是对同一列表的引用,因此,当从列表中删除时,您在列表上方。根据您的解释@NiceChang当元组中的-1或反向元组存在时,您的整个列表似乎将被删除?删除元组必须满足什么条件?当元组中的-1或相同但顺序不同的元组时。如1,2和2,1,然后我需要移除其中一个。这能回答你的问题吗?请记住,进程和匹配的_边是对同一列表的引用,因此在从列表中删除时,您处于列表上方。根据您的解释@NiceChang,当-1在元组或反向元组中存在时,您的整个列表似乎都将被删除?请告诉我:请告诉我: