简体   繁体   中英

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 . 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. 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

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.

Ok...So from ur ques, I understand that u wanna remove the repeated values from result . I tried to complete this task using list.remove , but I couldn't. So I have done it in a different way. Instead of using list.remove , add these lines to ur code:

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. 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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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