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

后端 未结 7 1073
深忆病人
深忆病人 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:21

    Might not be as fast as internal implementations, but takes (almost) linear time (since set lookup is logarithmic)

    mylist = ['A','A','B','C','D','E','D']
    myset = set()
    dups = set()
    for x in mylist:
        if x in myset:
            dups.add(x)
        else:
            myset.add(x)
    dups = list(dups)
    print dups
    

提交回复
热议问题