简体   繁体   English

将列表的每个项目与其余项目进行比较,并在迭代时从列表中删除匹配的元素

[英]compare each items of a list with the rest and remove matched elements from the list while iterating

My input data is我的输入数据是

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

My expected output is:我的预期输出是:

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

With the help of How to compare each item in a list with the rest, only once?借助如何将列表中的每个项目与其余项目进行比较,仅一次? currently my snippet looks like目前我的片段看起来像

mylist = [[1, 2], [3, 4], [5, 6], [1, 2, 7], [8, 2], [9, 5]]
result = list()

for i in range(len(mylist)):
    result.append(mylist[i])
    for j in range(i + 1, len(mylist)):
        if set(mylist[i]) & set(mylist[j]):
            result.append(mylist[j])
            mylist.remove(mylist[j])

print(result)

However, It is throwing error IndexError: list index out of range .但是,它抛出错误IndexError: list index out of range I guess this is because I am trying to remove items from a list while iterating.我想这是因为我试图在迭代时从列表中删除项目。

So I checked How to remove items from a list while iterating?所以我检查了如何在迭代时从列表中删除项目? . . It suggested using slice or itertools.它建议使用 slice 或 itertools。 It also gave a code snippet which I found much more readable.它还提供了一个代码片段,我发现它更具可读性。

temp = []
while somelist:
    x = somelist.pop()
    if not determine(x):
        temp.append(x)
while temp:
    somelist.append(templist.pop())

However, I could not figure out how it might work.但是,我无法弄清楚它是如何工作的。 Any idea?任何的想法?

Update 1更新 1

Snippet:片段:

mylist = [[1, 2], [3, 4], [5, 6], [1, 2, 7], [8, 2], [9, 5]]
result = list()

for i in range(len(mylist)):
    result.append(mylist[i])
    for j in range(i + 1, len(mylist)):
        if set(mylist[i]) & set(mylist[j]):
            result.append(mylist[j])
            # mylist.remove(mylist[j])

print(result)

Output:输出:

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

I do not want [1, 2, 7], [8, 2], [8, 2], [9, 5] in the result so I trying to use mylist.remove(mylist[j]) , which I could not figure out how to do.我不想要结果中的[1, 2, 7], [8, 2], [8, 2], [9, 5]所以我尝试使用mylist.remove(mylist[j]) ,我可以想不通怎么办。

Ok...So from ur ques, I understand that u wanna remove the repeated values from result .好的......所以从你的问题来看,我知道你想从result删除重复的值。 I tried to complete this task using list.remove , but I couldn't.我尝试使用list.remove完成此任务,但我不能。 So I have done it in a different way.所以我以不同的方式做到了。 Instead of using list.remove , add these lines to ur code:不要使用list.remove ,而是将这些行添加到您的代码中:

import copy
curr_lst = []

resultdup = copy.deepcopy(result)

for x in range(len(resultdup)):
    curr_lst = resultdup[x]
    for y in range(x+1,len(resultdup)):
        if curr_lst == resultdup[y]:
            indices = [i for i, x in enumerate(result) if x == curr_lst]
            for x in indices[1:]:
                del result[x]

print(result)

Output:输出:

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

Hope that this helps u.希望这对你有帮助。

OP Here, I could not find the answer of the question I originally asked. OP在这里,我找不到我最初提出的问题的答案。 However, I found an alternative way (though it might not be as optimal as removing the element from mylist[j] ) to achieve the result I was expecting.但是,我找到了一种替代方法(尽管它可能不如从mylist[j]删除元素那样最佳)来实现我期望的结果。

The code I am currently using is:我目前使用的代码是:

mylist = [[1, 2], [3, 4], [5, 6], [1, 2, 7], [8, 2], [9, 5]]
temp_result = list()

for i in range(len(mylist)):
    temp_result.append(mylist[i])
    for j in range(i + 1, len(mylist)):
        if (set(mylist[i]) & set(mylist[j])):
            temp_result.append(mylist[j])

result = []
for elem in temp_result:
    if elem not in result:
        result.append(elem)

print(result)

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

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