简体   繁体   中英

Remove all occurrences of item(s) in list if it appears more than once

I need help with a coding challenge that is asking to remove all occurrences of an item within a list that appear more than once. My code only removes one occurrence. It will not remove the item completely.

def solution(data, n):
    for x in data:
        while data.count(x) > 1:
            data.remove(x)
            continue        
    print(data)

solution([1, 2, 2, 3, 3, 4, 5, 5], 1)
expected result: [1, 4]
actual restult: [1, 2, 3, 4, 5]

you can use collections.Counter to count all instances of each element in data , then print only the relevant ones based on this count:

from collections import Counter


def solution(data, n):
    histogram = Counter(data)
    print([d for d in data if histogram[d] <= n])

solution([1, 2, 2, 3, 3, 4, 5, 5], 1)

or just use data.count() directly:

def solution(data, n):
    print([d for d in data if data.count(d) <= n])

solution([1, 2, 2, 3, 3, 4, 5, 5], 1)

As this is a coding challenge, it might be better to not rely upon Python-specific stuff, like collections or the count() function.

You could generically do this in any language using a hash table, which in Python is just a dictionary.

I assume the presence of the parameter n is to make the threshold above which you remove the number configurable, and not simply hard-coded to 1.

data = [1, 2, 2, 3, 3, 4, 5, 5]

def solution(data, n):
    occurrences = {}
    # count occurrences
    for item in data:
        occurrences[item] = occurrences.get(item, 0) + 1
    # return trimmed data
    trimmed_data = []
    for item in data:
        if occurrences[item] <= n:
            trimmed_data.append(item)
    return trimmed_data

solution(data, 1)

yields [1, 4]

This has O(n) time complexity.

You can save the second list by re-using the first one:

def solution(data, n):
    occurrences = {}
    # count occurrences
    for item in data:
        occurrences[item] = occurrences.get(item, 0) + 1
    # return trimmed data
    data.clear()
    for k, v in occurrences.items():
        if v <= n:
            data.append(k)
    return data

You could do:

def solution(data, n):
    for x in set(data):
        if data.count(x) > n:
            while x in data:
                data.remove(x)
    return data

Some explanation:

  • set(data) makes that every value is checked only once, even if it occurs multiple times in data ;
  • you should remove the continue statement, it does not do what you probably want it to do;
  • look carefully what I am doing with the if statement and the while statement;
  • in your code, the n argument was not used, I assumed that it should replace the 1 in the if -line.

Update: Here is the one line version!

solution = lambda data, n: [x for x in set(data) if data.count(x) <= n]

You can call the solution function the same as before. If you are puzzled about this syntax, you could search 'python list comprehensions' and 'python lambda'.

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