简体   繁体   中英

There is a list from which i need to find duplicates

I have written a code, which is able to do so. However, i am not able to find the exact value. The code i have written is sharing all values with me, instead of just 1 duplicate value

some_list=['a','b','c','b','d','m','n','n']
a=[]
for items in some_list:
  if some_list.count(items) > 1:
      a.append(items)
print (a)
some_list=['a','b','c','b','d','m','n','n']
a=[]
for items in some_list:
    if some_list.count(items) > 1:
        if items not in a:
            a.append(items)
print (a)

output:

['b', 'n']

or:

some_list=['a','b','c','b','d','m','n','n']
a=[]
for items in some_list:
    if some_list.count(items) > 1:

        a.append(items)
a=set(a)
print (a)

out:

['b', 'n']

Your code is appending all duplicates value to new list ie 'a'. To keep only 1 duplicate value, you need to check whether the same value is already there in your list or not. If this is not there, then only you should append that value to your list. I have modified your code, to suite this need .

enter code here
some_list=['a','b','c','b','d','m','n','n']
a=[]
for items in some_list:
  if some_list.count(items) > 1 and a.count(items) == 0:
      a.append(items)
print (a)

Output:

['b', 'n']

We can utilize Counter module from Collections package.

from collections import Counter
final_list = []
cntr = Counter(['a','b','c','b','d','m','n','n'])
for i in cntr.iteritems():
    if i[1] > 1:
        final_list.append(i[0])
print final_list

For more info on Counter module please go through https://docs.python.org/2/library/collections.html#collections.Counter

Counter and defaultdict data structures from collections module perfectly suits the use-case, here is another approach using normal dict data structure which may be more intuitive for beginners in understanding the approach.

Approach :

Step-1: Building a dictionary with item as key and its occurrences as value.

Step-2 Filter out items whose occurrences is less than 2, in other words, select items whose occurrences are greater than 1.

def get_duplicates(some_list): 
    frequency_counter = {}

    # Step-1
    for item in some_list:
        if item in frequency_counter:
            frequency_counter[item] += 1
        else:
            frequency_counter[item] = 1

    # Step-2
    return [item for item, count in frequency_counter.items() if count > 1]

In [2]: get_duplicates(['a','b','c','b','d','m','n','n'])                                                                                     
Out[2]: ['b', 'n']

you can use list comprehension with collections.Counter :

from collections import Counter

[e for e, freq in Counter(some_list).items() if freq > 1]

output:

['b', 'n']

another approach will be to sort your list:

previous = None
elemetns = set()
for i in sorted(some_list):
    if previous == i:
        elemetns.update(i)
    previous = i

print(elements)

output:

{'b', 'n'}

Your code will work,If you'd add one more condition in your if Statement.

some_list = ['a', 'b', 'c', 'b', 'd', 'm', 'n', 'n']
a = []
for items in some_list:
    if some_list.count(items) > 1 and items not in a:
        a.append(items)
print (a)

Output:-

['b', 'n']

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