如何遍历列表并一路删除值

时间:2020-07-31 17:44:46

标签: python

我目前正在建立某种配对系统,我想遍历列表并将列表中的人匹配起来。但是我不知道如何在迭代时摆脱值(A和B),并且不会引发“ list.remove(x):x不在列表错误”。有谁知道我可以做到这一点?

for A in SORTEDUsers:
    for B in SORTEDUsers:
        if A[1] == (B[1]-1) or A == (B[1]) or A == (B[1]+1) and (B[2] in A[3]) and A[5] in B[5] and A != B:
            print(A[0],B[0])

1 个答案:

答案 0 :(得分:0)

如果您制作另一个数组或列表来表示是否选择了某个人,该如何呢?

selected=[False]*len(SORTEDUsers)
for i in range(len(SORTEDUsers)):
    if not selected[i]:
        A=SORTEDUsers[i]
        for j in range(len(SORTEDUsers)):
            if not selected[j]:
                B=SORTEDUsers[j]
                if A[1] == (B[1]-1) or A == (B[1]) or A == (B[1]+1) and (B[2] in A[3]) and A[5] in B[5] and A != B:
                    print(A[0],B[0])
                    selected[i]=True
                    selected[j]=True