CheckIO (Python)> Non-unique Elements> I implemented it

https://py.checkio.org/mission/non-unique-elements/

The process of removing non-unique elements from the list. As an example, it returns [1, 3, 1, 3] for [1, 2, 3, 1, 3].

v0.1

I passed all assertions below, but the description is complicated.

from collections import Counter

def checkio(data):
    print(list(data))

    counter = Counter(data)
    pickups = []
    for num, cnt in counter.most_common():
        if cnt > 1:
    		pickups.append(num)
    
    reslist = []
    for elem in data:
    	for flt in pickups:
    		if elem is flt:
    			reslist.append(elem)
    			
    #print(list(reslist))
    return reslist

#Some hints
#You can use list.count(element) method for counting.
#Create new list with non-unique elements
#Loop over original list


if __name__ == "__main__":
    #These "asserts" using only for self-checking and not necessary for auto-testing
    assert list(checkio([1, 2, 3, 1, 3])) == [1, 3, 1, 3], "1st example"
    assert list(checkio([1, 2, 3, 4, 5])) == [], "2nd example"
    assert list(checkio([5, 5, 5, 5, 5])) == [5, 5, 5, 5, 5], "3rd example"
    assert list(checkio([10, 9, 10, 10, 9, 8])) == [10, 9, 10, 10, 9], "4th example"
    print("It is all good. Let's check it now")

Even if you look at the suggestion, it says that you should use list.append (), so I can't think of any improvement.

v0.2

I decided to stop adding and remove it.

from collections import Counter

def checkio(data):
    print(list(data))

    counter = Counter(data)
    removes = []
    for num, cnt in counter.most_common():
        if cnt == 1:
            removes.append(num)
    
    for elem in removes:
        data.remove(elem)
        
    #print(list(reslist))
    return data

#Some hints
#You can use list.count(element) method for counting.
#Create new list with non-unique elements
#Loop over original list


if __name__ == "__main__":
    #These "asserts" using only for self-checking and not necessary for auto-testing
    assert list(checkio([1, 2, 3, 1, 3])) == [1, 3, 1, 3], "1st example"
    assert list(checkio([1, 2, 3, 4, 5])) == [], "2nd example"
    assert list(checkio([5, 5, 5, 5, 5])) == [5, 5, 5, 5, 5], "3rd example"
    assert list(checkio([10, 9, 10, 10, 9, 8])) == [10, 9, 10, 10, 9], "4th example"
    print("It is all good. Let's check it now")
    

Recommended Posts

CheckIO (Python)> Non-unique Elements> I implemented it
I implemented Python Logging
[Python] I implemented peripheral Gibbs sampling
CheckIO (Python)> House Password> I implemented it> PEP8: do not assign a lambda expression, use a def
Wrangle x Python book I tried it [2]
I implemented Cousera's logistic regression in Python
Wrangle x Python book I tried it [1]
[Python] I introduced Word2Vec and played with it.
I implemented Robinson's Bayesian Spam Filter in python
When I try matplotlib in Python, it says'cairo.Context'
I started python
I implemented CycleGAN (1)
I implemented the inverse gamma function in python
I implemented ResNet!
I implemented breadth-first search in python (queue, drawing self-made)
Since memory_profiler of python is heavy, I measured it
I wondered if Python 3.4 was faster, but it was slower
AWS Lambda now supports Python so I tried it
I was able to repeat it in Python: lambda
I implemented a Vim-like replacement command in Slackbot #Python
[Streamlit] I installed it
Implemented SimRank in Python
Python Not Implemented Error
I tried Python> autopep8
Qiskit: I implemented VQE
Implemented Matrix Factorization (python)
I was able to repeat it in Python: lambda
I implemented Cousera's logistic regression in Python
CheckIO (Python)> Non-unique Elements> I implemented it
Implemented DQN in TensorFlow (I wanted to ...)
I tried Python> decorator
Why I chose Python
Implemented Shiritori in Python
I compared Python more-itertools 2.5 → 2.6
[Python] I installed the game from pip and played it
[I made it with Python] XML data batch output tool
I implemented Donald Knuth's unbiased sequential calculation algorithm in Python