简体   繁体   中英

how to find multiple non-repeating numbers in python?

I have a below method that finds the non-repeating elements in a list:

def dupes(a):
    s = {}
    for ele in a:
        if ele not in s:
            s[ele] = 1
        else:
            s[ele] += 1
    for x in s:
        if s[x] == 1:
            return 'this is the only non-repeating element value is :', s[x], 'and the key is :', x
    return

l = [4, 7, 4, 5, 7, 6, 5, 6, 10]
cd = dupes(l)
print("This is dupes: ", cd)

The code runs successfully and below is the output:

This is dupes:  ('this is the only non-repeating element value is :', 1, 'and the key is :', 10)

But when I am trying to add more than one non-repeating elements in the list, the output doesn't change. For example, if I add 11 at the end of the list the output still remains the same as above..

Any idea?

You can get all keys whose value is 1 using a list comprehension.

nondupes = [k for (k,v) in s.items() if v==1]
return 'Non-repeating elements: ' + str(nondupes)

Also, you could replace all your counting code with a collections.Counter :

s = Counter(a)
def dupes(a):
    s = {}
    for ele in a:
        if ele not in s:
            s[ele] = 1
        else:
            s[ele] += 1
    count = 0
    keys = []
    for x in s:
        if s[x] == 1:
            count += 1
            keys.append(x)
    return 'this is the only non-repeating element value is :', count, 'and the key is :', keys

That would be fine.

Actually when you return at the 10th line, the function ends. This is in case you don't understand the list comprehension yet, as many have given you the solution with that technique. I will just give you a simple solution where it will return a list of non-repeating numbers.

def dupes(a):
    s = {}
    non_dupes = []
    for ele in a:
        if ele not in s:
            s[ele] = 1
        else:
            s[ele] += 1
    for x in s:
        if s[x] == 1:
            non_dupes.append(x)
    return non_dupes

l = [4, 7, 4, 5, 7, 6, 5, 6, 10, 11]
cd = dupes(l)
print("This is dupes: ", cd)

You can succinctly achieve the above with Counter :

from collections import Counter
l = [4, 7, 4, 5, 7, 6, 5, 6, 10]
c = Counter(l)
dupes = list(key for key in c if c[key] > 1)
print("This is dupes: ", dupes)

Output:

This is dupes:  [4, 7, 5, 6]

The issue that you are encountering is that you are returning prematurely from your function with a string containing the first duplicate. The rest of the duplicates are lost.

You already have the list of duplicates, let's return just the data so we can separate the calculation of results from the display of results.

return list(e for e in s if s[e] > 1)

You can easely solve this in one line of python code, using the count() function:

l = [4, 7, 4, 5, 7, 6, 5, 6, 10 ,11]

only_one_time = [e for e in l if l.count(e) < 2]

print(only_one_time)

The given code returns the first non-repeating element it encounters. For example, if the input list is l = [12, 11, 4, 7, 4, 5, 7, 6, 5, 6, 10] , the output would be This is dupes: ('this is the only non-repeating element value is:', 1, 'and the key is:', 12) .

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