吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 639|回复: 0
收起左侧

[讨论] 根据容器元素生成字典-改进方案

[复制链接]
青衫桑 发表于 2022-3-26 11:36
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为等差数列的字典,想看看有没有优化改进方案。

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止回复与主题无关非技术内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-15 21:15

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表