Merge pull request #176 from mx-psi/filterimprov
[FIX: #170] Improve filter functions
This commit is contained in:
@ -5,11 +5,14 @@ tags: list,beginner
|
||||
|
||||
Filters out the non-unique values in a list.
|
||||
|
||||
Use list comprehension and `list.count()` to create a list containing only the unique values.
|
||||
Use a `Counter` to get the count of each value in the list.
|
||||
Use list comprehension to create a list containing only the unique values.
|
||||
|
||||
```py
|
||||
from collections import Counter
|
||||
|
||||
def filter_non_unique(lst):
|
||||
return [item for item in lst if lst.count(item) == 1]
|
||||
return [item for item, count in counter = Counter(lst).items() if count == 1]
|
||||
```
|
||||
|
||||
```py
|
||||
|
||||
@ -5,11 +5,14 @@ tags: list,beginner
|
||||
|
||||
Filters out the unique values in a list.
|
||||
|
||||
Use list comprehension and `list.count()` to create a list containing only the non-unique values.
|
||||
Use a `Counter` to get the count of each value in the list.
|
||||
Use list comprehension to create a list containing only the non-unique values.
|
||||
|
||||
```py
|
||||
from collections import Counter
|
||||
|
||||
def filter_unique(lst):
|
||||
return [x for x in set(item for item in lst if lst.count(item) > 1)]
|
||||
return [item for item, count in Counter(lst).items() if count > 1]
|
||||
```
|
||||
|
||||
```py
|
||||
|
||||
Reference in New Issue
Block a user