Identify duplicate values in a list in Python

前端 未结 12 1886
渐次进展
渐次进展 2020-11-29 06:31

Is it possible to get which values are duplicates in a list using python?

I have a list of items:

    mylist = [20, 30, 25, 20]

I k

相关标签:
12条回答
  • 2020-11-29 07:02

    That's the simplest way I can think for finding duplicates in a list:

    my_list = [3, 5, 2, 1, 4, 4, 1]
    
    my_list.sort()
    for i in range(0,len(my_list)-1):
                   if my_list[i] == my_list[i+1]:
                       print str(my_list[i]) + ' is a duplicate'
    
    0 讨论(0)
  • 2020-11-29 07:03

    I tried below code to find duplicate values from list

    1) create a set of duplicate list

    2) Iterated through set by looking in duplicate list.

    glist=[1, 2, 3, "one", 5, 6, 1, "one"]
    x=set(glist)
    dup=[]
    for c in x:
        if(glist.count(c)>1):
            dup.append(c)
    print(dup)
    

    OUTPUT

    [1, 'one']

    Now get the all index for duplicate element

    glist=[1, 2, 3, "one", 5, 6, 1, "one"]
    x=set(glist)
    dup=[]
    for c in x:
        if(glist.count(c)>1):
            indices = [i for i, x in enumerate(glist) if x == c]
            dup.append((c,indices))
    print(dup)
    

    OUTPUT

    [(1, [0, 6]), ('one', [3, 7])]

    Hope this helps someone

    0 讨论(0)
  • 2020-11-29 07:06

    Here's a list comprehension that does what you want. As @Codemonkey says, the list starts at index 0, so the indices of the duplicates are 0 and 3.

    >>> [i for i, x in enumerate(mylist) if mylist.count(x) > 1]
    [0, 3]
    
    0 讨论(0)
  • 2020-11-29 07:08

    These answers are O(n), so a little more code than using mylist.count() but much more efficient as mylist gets longer

    If you just want to know the duplicates, use collections.Counter

    from collections import Counter
    mylist = [20, 30, 25, 20]
    [k for k,v in Counter(mylist).items() if v>1]
    

    If you need to know the indices,

    from collections import defaultdict
    D = defaultdict(list)
    for i,item in enumerate(mylist):
        D[item].append(i)
    D = {k:v for k,v in D.items() if len(v)>1}
    
    0 讨论(0)
  • 2020-11-29 07:09

    simplest way without any intermediate list using list.index():

    z = ['a', 'b', 'a', 'c', 'b', 'a', ]
    [z[i] for i in range(len(z)) if i == z.index(z[i])]
    >>>['a', 'b', 'c']
    

    and you can also list the duplicates itself (may contain duplicates again as in the example):

    [z[i] for i in range(len(z)) if not i == z.index(z[i])]
    >>>['a', 'b', 'a']
    

    or their index:

    [i for i in range(len(z)) if not i == z.index(z[i])]
    >>>[2, 4, 5]
    

    or the duplicates as a list of 2-tuples of their index (referenced to their first occurrence only), what is the answer to the original question!!!:

    [(i,z.index(z[i])) for i in range(len(z)) if not i == z.index(z[i])]
    >>>[(2, 0), (4, 1), (5, 0)]
    

    or this together with the item itself:

    [(i,z.index(z[i]),z[i]) for i in range(len(z)) if not i == z.index(z[i])]
    >>>[(2, 0, 'a'), (4, 1, 'b'), (5, 0, 'a')]
    

    or any other combination of elements and indices....

    0 讨论(0)
  • 2020-11-29 07:09

    mylist = [20, 30, 25, 20]

    kl = {i: mylist.count(i) for i in mylist if mylist.count(i) > 1 }

    print(kl)

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