Delete unique elements from a list

前端 未结 9 1224
[愿得一人]
[愿得一人] 2020-12-10 09:20

I faced some problem with solving the next problem:

We have a list of elements (integers), and we should return a list consisting of only the non-unique elements in

相关标签:
9条回答
  • 2020-12-10 10:02

    Try this:

    >>> a=[1,2,3,3,4,5,6,6,7,8,9,2,0,0]
    >>> a=[i for i in a if a.count(i)>1]
    >>> a
    [2, 3, 3, 6, 6, 2, 0, 0]
    >>> a=[1, 2, 3, 1, 3]
    >>> a=[i for i in a if a.count(i)>1]
    >>> a
    [1, 3, 1, 3]
    >>> a=[1, 2, 3, 4, 5]
    >>> a=[i for i in a if a.count(i)>1]
    a
    []
    
    0 讨论(0)
  • 2020-12-10 10:04

    I used an integer and bool to check every time the list was modified within a while loop.

    rechecks = 1
    runscan = True
    while runscan == True:
        for i in data:
             if data.count(i) <2:
                 data.remove(i)
                 rechecks+=1
                #need to double check now
        if rechecks >0:
            runscan = True
            rechecks-=1
        else:
            runscan = False       
    return data
    
    0 讨论(0)
  • 2020-12-10 10:06

    Following what you have started, iterating on the list of integers, but not counting or deleting elements, try just testing if the element has already been seen, append it to a list of duplicated elements:

    def checkio(data):
        elements = []
        duplicates = []
        for i in data:
            if i not in elements:
                elements.append(i)
            else:
                if i not in duplicates:
                    duplicates.append(i)
        return duplicates
    
    d = [1, 3, 1, 2, 3, 5, 8, 1, 5, 2]
    
    print (checkio(d))
    #[1, 3, 5, 2]
    
    0 讨论(0)
  • 2020-12-10 10:10

    You can implement a OrderedCounter, eg:

    from collections import OrderedDict, Counter
    
    class OrderedCounter(Counter, OrderedDict): 
        pass
    
    data = [1, 3, 1, 2, 3, 5, 8, 1, 5, 2]
    
    duplicates = [k for k, v in OrderedCounter(data).items() if v > 1]
    # [1, 3, 2, 5]
    

    So you count the occurrence of each value, then filter on if it has a frequency of more than one. Inheriting from OrderedDict means the order of the original elements is preserved.


    Going by comments, you want all duplicated elements reserved, so you can pre-build a set of the duplicate entries, then re-iterate your original list, eg:

    from collections import Counter
    
    data = [1, 3, 1, 2, 3, 5, 8, 1, 5, 2]
    duplicates = {k for k, v in Counter(data).items() if v > 1}
    result = [el for el in data if el in duplicates]
    # [1, 3, 1, 2, 3, 5, 1, 5, 2]
    
    0 讨论(0)
  • 2020-12-10 10:10
    def checkio(data):
        lis = []
        for i in data:
            if data.count(i)>1:
                lis.append(i)
        print(lis)
    checkio([1,2,3,3,2,1])
    

    Yeah it's a bit late to contribute to this thread but just wanted to put it there on the net for anyone else use.

    0 讨论(0)
  • 2020-12-10 10:14
     numbers = [1, 1, 1, 1, 3, 4, 9, 0, 1, 1, 1]
     x=set(numbers)
     print(x)
    

    You can use the set key word too to get the desired solution.

    0 讨论(0)
提交回复
热议问题