Get file name and extension
from pathlib import Path
path = Path("dir/file.xml")
file_name = path.stem # file
file_extension = path.suffix # .xml
Iterate through directory contents
from pathlib import Path
data_root = Path("/imgs")
for item in data_root.iterdir():
print(item)
You can also use glob
from pathlib import Path
data_root = Path("/imgs")
# Get all png images in this directory level
data_root.glob("*.png")
# Get all png images in this directory including sub-directories
data_root.glob("*.png", recursive=True)
Inverse dictionary key/value => value/key
inversed_dict = {v: k for k, v in some_dict.items()}
Flatten a list of lists
import operator
flat_list = reduce(operator.concat, some_list)
Map string list to int list
elements = ["1", "2", "3"]
elements = list(map(int, elements))
Count unique values in list and their total
from collections import Counter
elements = ["1", "2", "3", "2", "3", "3"]
# Unique values in list
keys = Counter(elements).keys()
# Frequency of their appearance
values = Counter(elements).values()
Sort dict by value
for key in sorted(confidences, key=confidences.get, reverse=True):
print(f'key {key}')
Format f strings
print(f'2 decimal points {some_variable:.2f}')
Batch an iterable
def batch(iterable, n=1):
l = len(iterable)
for index in range(0, l, n):
yield iterable[index:min(index + n, l)]
# and call it like
for x, pred in enumerate(batch(preds, 32), 1):
pass
I am working on a project called ML Studio, want to get early access to and product updates? Subscribe here or follow me on twitter.
Top comments (0)