from typing import Generator
from typing import Iterable
def index_dict(
item: list | tuple | Generator | Iterable,
start: int = 0,
step: int = 1) -> dict:
"""
Generate a dictionary whose key is an equal difference sequence according to the container.
parameter item: Containers containing elements, which can be lists, tuples, generators, iterators.
parameter start: First number.
parameter step: Digital interval.
"""
if not isinstance(item, (list, tuple, Generator, Iterable)):
raise TypeError("The type of item is not supported")
if step == 0:
raise ValueError("step must not be zero")
return {start + x * step: y for x, y in enumerate(item)}
这个代码是根据传入的列表元素生成一个key为等差数列的字典,想看看有没有优化改进方案。
|