Update some snippets
This commit is contained in:
@ -1,10 +1,12 @@
|
||||
---
|
||||
title: capitalize
|
||||
tags: string
|
||||
tags: string,intermediate
|
||||
---
|
||||
|
||||
Capitalizes the first letter of a string.
|
||||
|
||||
Capitalizes the first letter of the string and then adds it with rest of the string. Omit the `lower_rest` parameter to keep the rest of the string intact, or set it to `true` to convert to lowercase.
|
||||
Capitalize the first letter of the string and then add it with rest of the string.
|
||||
Omit the `lower_rest` parameter to keep the rest of the string intact, or set it to `True` to convert to lowercase.
|
||||
|
||||
```py
|
||||
def capitalize(string, lower_rest=False):
|
||||
|
||||
@ -1,10 +1,11 @@
|
||||
---
|
||||
title: capitalize_every_word
|
||||
tags: string
|
||||
tags: string,beginner
|
||||
---
|
||||
|
||||
Capitalizes the first letter of every word in a string.
|
||||
|
||||
Uses `str.title` to capitalize first letter of every word in the string.
|
||||
Use `string.title()` to capitalize first letter of every word in the string.
|
||||
|
||||
```py
|
||||
def capitalize_every_word(string):
|
||||
|
||||
@ -1,15 +1,17 @@
|
||||
---
|
||||
title: chunk
|
||||
tags: list
|
||||
tags: list,intermediate
|
||||
---
|
||||
Chunks an list into smaller lists of a specified size.
|
||||
|
||||
Uses `range` to create a list of desired size. Then use `map` on this list and fill it with splices of `lst`.
|
||||
Chunks a list into smaller lists of a specified size.
|
||||
|
||||
Use `list()` and `range()` to create a list of the desired `size`.
|
||||
Use `map()` on the list and fill it with splices of the given list.
|
||||
Finally, return use created list.
|
||||
|
||||
```py
|
||||
from math import ceil
|
||||
|
||||
|
||||
def chunk(lst, size):
|
||||
return list(
|
||||
map(lambda x: lst[x * size:x * size + size],
|
||||
|
||||
@ -1,10 +1,11 @@
|
||||
---
|
||||
title: compact
|
||||
tags: list
|
||||
tags: list,beginner
|
||||
---
|
||||
|
||||
Removes falsey values from a list.
|
||||
|
||||
Use `filter()` to filter out falsey values (False, None, 0, and "").
|
||||
Use `filter()` to filter out falsey values (`False`, `None`, `0`, and `""`).
|
||||
|
||||
```py
|
||||
def compact(lst):
|
||||
|
||||
@ -1,12 +1,12 @@
|
||||
---
|
||||
title: count_by
|
||||
tags: list
|
||||
tags: list,intermediate
|
||||
---
|
||||
:information_source: Already implemented via `collections.Counter`
|
||||
|
||||
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.
|
||||
Use `map()` to map the values of the given list using the given function.
|
||||
Iterate over the map and increase the element count each time it occurs.
|
||||
|
||||
```py
|
||||
def count_by(arr, fn=lambda x: x):
|
||||
|
||||
Reference in New Issue
Block a user