Merge pull request #22 from huybery/master

FIX: #20
This commit is contained in:
Rohit Tanwar
2018-02-27 11:04:49 +05:30
committed by GitHub
2 changed files with 5 additions and 10 deletions

View File

@ -1,7 +1,7 @@
average:[Rohit Tanwar](@kriadmin),[Hui Binyuan](@huybery)
chunk:[Rohit Tanwar](@kriadmin)
compact:[Rohit Tanwar](@kriadmin)
count_occurences:[Rohit Tanwar](@kriadmin)
count_occurences:[Rohit Tanwar](@kriadmin), [Hui Binyuan](@huybery)
count_vowels:[Rohit Tanwar](@kriadmin)
deep_flatten:[Rohit Tanwar](@kriadmin),[Meet Zaveri](@meetzaveri)
difference:[Rohit Tanwar](@kriadmin)

View File

@ -4,18 +4,13 @@
Counts the occurrences of a value in an list.
Uses the `reduce` functin from built-in module `functools` to increment a counter each time you encounter the specific value inside the list.
Uses the list comprehension to increment a counter each time you encounter the specific value inside the list.
```python
from functools import reduce
def count_occurences(arr, val):
return reduce(
(lambda x, y: x + 1 if y == val and type(y) == type(val) else x + 0),
arr)
def count_occurrences(lst, val):
return len([x for x in lst if x == val and type(x) == type(val)])
```
```python
count_occurrences([1, 1, 2, 1, 2, 3], 1) # 3
```
```