22 lines
655 B
Python
22 lines
655 B
Python
import typing
|
|
from itertools import islice
|
|
|
|
|
|
def chunks(items: list | dict, n: int) -> typing.Generator:
|
|
"""Yield successive n-sized chunks from lst."""
|
|
|
|
if isinstance(items, list):
|
|
for i in range(0, len(items), n):
|
|
yield items[i:i + n]
|
|
elif isinstance(items, dict):
|
|
it = iter(items)
|
|
for i in range(0, len(items), n):
|
|
yield {k: items[k] for k in islice(it, n)}
|
|
|
|
|
|
def evenly_spaced_elements(lst: list, count: int) -> list:
|
|
if count > len(lst):
|
|
return lst
|
|
interval = (len(lst) - 1) / (count - 1) if count > 1 else 0
|
|
return [lst[int(round(i * interval))] for i in range(count)]
|