Get a unique list of items that occur more than once in a list

后端 未结 7 1058
深忆病人
深忆病人 2020-12-03 17:51

I have a list of items:

mylist = [\'A\',\'A\',\'B\',\'C\',\'D\',\'E\',\'D\']

I want to return a unique list of items that appear more than

相关标签:
7条回答
  • 2020-12-03 18:32

    Try some thing like this:

    a = ['A','A','B','C','D','E','D']
    
    import collections
    print [x for x, y in collections.Counter(a).items() if y > 1]
     ['A', 'D']
    

    Reference: How to find duplicate elements in array using for loop in Python?

    OR

    def list_has_duplicate_items( mylist ):
        return len(mylist) > len(set(mylist))
    def get_duplicate_items( mylist ):
        return [item for item in set(mylist) if mylist.count(item) > 1]
    mylist = [ 'oranges' , 'apples' , 'oranges' , 'grapes' ]
    print 'List: ' , mylist
    print 'Does list have duplicate item(s)? ' , list_has_duplicate_items( mylist )
    print 'Redundant item(s) in list: ' , get_duplicate_items( mylist )
    

    Reference https://www.daniweb.com/software-development/python/threads/286996/get-redundant-items-in-list

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