Update some snippets
This commit is contained in:
@ -1,16 +1,15 @@
|
||||
---
|
||||
title: count_occurences
|
||||
tags: list
|
||||
tags: list,beginner
|
||||
---
|
||||
:information_source: Already implemented via `list.count()`.
|
||||
|
||||
Counts the occurrences of a value in a list.
|
||||
|
||||
Uses the list comprehension to increment a counter each time you encounter the specific value inside the list.
|
||||
Increment a counter for every item in the list that has the given value and is of the same type.
|
||||
|
||||
```py
|
||||
def count_occurrences(lst, val):
|
||||
return len([x for x in lst if x == val and type(x) == type(val)])
|
||||
return len([x for x in lst if x == val and type(x) == type(val)])
|
||||
```
|
||||
|
||||
```py
|
||||
|
||||
@ -1,17 +0,0 @@
|
||||
---
|
||||
title: count_vowels
|
||||
tags: string
|
||||
---
|
||||
Retuns `number` of vowels in provided `string`.
|
||||
|
||||
Use a regular expression to count the number of vowels `(A, E, I, O, U)` in a string.
|
||||
|
||||
```py
|
||||
import re
|
||||
|
||||
def count_vowels(str):
|
||||
return len(re.findall(r'[aeiou]', str, re.IGNORECASE))
|
||||
|
||||
count_vowels('foobar') # 3
|
||||
count_vowels('gym')# 0
|
||||
```
|
||||
@ -1,14 +1,16 @@
|
||||
---
|
||||
title: decapitalize
|
||||
tags: string
|
||||
tags: string,intermediate
|
||||
---
|
||||
|
||||
Decapitalizes the first letter of a string.
|
||||
|
||||
Decapitalizes the first letter of the string and then adds it with rest of the string. Omit the `upper_rest` parameter to keep the rest of the string intact, or set it to `true` to convert to uppercase.
|
||||
Decapitalize the first letter of the string and then add it with rest of the string.
|
||||
Omit the `upper_rest` parameter to keep the rest of the string intact, or set it to `True` to convert to uppercase.
|
||||
|
||||
```py
|
||||
def decapitalize(string, upper_rest=False):
|
||||
return str[:1].lower() + (str[1:].upper() if upper_rest else str[1:])
|
||||
return str[:1].lower() + (str[1:].upper() if upper_rest else str[1:])
|
||||
```
|
||||
|
||||
```py
|
||||
|
||||
@ -1,27 +1,30 @@
|
||||
---
|
||||
title: deep_flatten
|
||||
tags: list
|
||||
tags: list,recursion,intermediate
|
||||
---
|
||||
|
||||
Deep flattens a list.
|
||||
|
||||
Use recursion. Use `list.extend()` with an empty list (`result`) and the spread function to flatten a list. Recursively flatten each element that is a list.
|
||||
Use recursion.
|
||||
Define a function, `spread`, that uses either `list.extend()` or `list.append()` on each element in a list to flatten it.
|
||||
Use `list.extend()` with an empty list and the `spread` function to flatten a list.
|
||||
Recursively flatten each element that is a list.
|
||||
|
||||
```py
|
||||
def spread(arg):
|
||||
ret = []
|
||||
for i in arg:
|
||||
if isinstance(i, list):
|
||||
ret.extend(i)
|
||||
else:
|
||||
ret.append(i)
|
||||
return ret
|
||||
|
||||
ret = []
|
||||
for i in arg:
|
||||
if isinstance(i, list):
|
||||
ret.extend(i)
|
||||
else:
|
||||
ret.append(i)
|
||||
return ret
|
||||
|
||||
def deep_flatten(lst):
|
||||
result = []
|
||||
result.extend(
|
||||
spread(list(map(lambda x: deep_flatten(x) if type(x) == list else x, lst))))
|
||||
return result
|
||||
result = []
|
||||
result.extend(
|
||||
spread(list(map(lambda x: deep_flatten(x) if type(x) == list else x, lst))))
|
||||
return result
|
||||
```
|
||||
|
||||
```py
|
||||
|
||||
@ -1,16 +1,18 @@
|
||||
---
|
||||
title: difference
|
||||
tags: list
|
||||
tags: list,beginner
|
||||
---
|
||||
|
||||
Returns the difference between two iterables.
|
||||
|
||||
Use list comprehension to only keep values not contained in `b`.
|
||||
Create a `set` fromn `b`, then use list comprehension on `a` to only keep values not contained in the previously created set, `_b`.
|
||||
|
||||
```py
|
||||
def difference(a, b):
|
||||
_b = set(b)
|
||||
return [item for item in a if item not in _b]
|
||||
_b = set(b)
|
||||
return [item for item in a if item not in _b]
|
||||
```
|
||||
|
||||
```py
|
||||
difference([1, 2, 3], [1, 2, 4]) # [3]
|
||||
```
|
||||
|
||||
@ -1,15 +1,16 @@
|
||||
---
|
||||
title: difference_by
|
||||
tags: list
|
||||
tags: list,function,intermediate
|
||||
---
|
||||
Returns the difference between two list, after applying the provided function to each list element of both.
|
||||
|
||||
Create a `set` by applying `fn` to each element in `b`, then use list comprehension in combination with fn on a to only keep values not contained in the previously created `set`.
|
||||
Returns the difference between two lists, after applying the provided function to each list element of both.
|
||||
|
||||
Create a `set` by applying `fn` to each element in `b`, then use list comprehension in combination with `fn` on `a` to only keep values not contained in the previously created set, `_b`.
|
||||
|
||||
```py
|
||||
def difference_by(a, b, fn):
|
||||
b = set(map(fn, b))
|
||||
return [item for item in a if fn(item) not in b]
|
||||
_b = set(map(fn, b))
|
||||
return [item for item in a if fn(item) not in _b]
|
||||
```
|
||||
|
||||
```py
|
||||
|
||||
Reference in New Issue
Block a user