count_by
This commit is contained in:
30
README.md
30
README.md
@ -29,6 +29,7 @@
|
||||
<li><a href = "#shuffle"><code>shuffle</code></a></li>
|
||||
<li><a href = "#spread"><code>spread</code></a></li>
|
||||
<li><a href = "#zip"><code>zip</code></a></li>
|
||||
<li><a href = "#count_by"><code>count_by</code></a></li>
|
||||
</ul></details>
|
||||
|
||||
### :scroll: String
|
||||
@ -489,6 +490,35 @@ zip(['a'], [1, 2], [True, False], fill_value = '_') # [['a', 1, True], ['_', 2,
|
||||
|
||||
<br><a href = "#table-of-contents">:arrow_up: Back to top</a>
|
||||
|
||||
### count_by
|
||||
|
||||
Groups the elements of a list based on the given function and returns the count of elements in each group.
|
||||
|
||||
Use `map()` to map the values of the list using the given function. Iterate over the map and increase the the elements count each time it occurs.
|
||||
|
||||
```py
|
||||
def count_by(arr, fn=lambda x: x):
|
||||
key = {}
|
||||
for el in map(fn, arr):
|
||||
key[el] = 0 if not el in key else key[el]
|
||||
key[el] += 1
|
||||
return key
|
||||
|
||||
```
|
||||
|
||||
<details><summary>View Examples</summary>
|
||||
|
||||
```py
|
||||
|
||||
from math import floor
|
||||
count_by([6.1, 4.2, 6.3], floor) # {4: 1, 6: 2}
|
||||
count_by(['one', 'two', 'three'], len) # {3: 2, 5: 1}
|
||||
|
||||
```
|
||||
</details>
|
||||
|
||||
<br><a href = "#table-of-contents">:arrow_up: Back to top</a>
|
||||
|
||||
## :scroll: String
|
||||
|
||||
### count_vowels
|
||||
|
||||
20
snippets/count_by.md
Normal file
20
snippets/count_by.md
Normal file
@ -0,0 +1,20 @@
|
||||
### count_by
|
||||
|
||||
Groups the elements of a list based on the given function and returns the count of elements in each group.
|
||||
|
||||
Use `map()` to map the values of the list using the given function. Iterate over the map and increase the the elements count each time it occurs.
|
||||
|
||||
```python
|
||||
def count_by(arr, fn=lambda x: x):
|
||||
key = {}
|
||||
for el in map(fn, arr):
|
||||
key[el] = 0 if not el in key else key[el]
|
||||
key[el] += 1
|
||||
return key
|
||||
```
|
||||
|
||||
``` python
|
||||
from math import floor
|
||||
count_by([6.1, 4.2, 6.3], floor) # {4: 1, 6: 2}
|
||||
count_by(['one', 'two', 'three'], len) # {3: 2, 5: 1}
|
||||
```
|
||||
@ -19,4 +19,5 @@ capitalize_every_word:string
|
||||
decapitalize:string
|
||||
palindrome:string
|
||||
is_upper_case:string
|
||||
is_lower_case:string
|
||||
is_lower_case:string
|
||||
count_by:list
|
||||
Reference in New Issue
Block a user