推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
css3
V2EX  ›  Python

python3 一小段 code 求个简写

  •  
  •   css3 · Nov 13, 2020 · 3631 views
    This topic created in 2192 days ago, the information mentioned may be changed or developed.

    大佬们,下面这小段,有什么简写吗,已被自己蠢哭

    # python3
    
    A_one, A_other_one, A_two, A_other_two = A
    B_one, B_other_one, B_two, B_other_two = B
    ... # 会有很多
    
    all = [
        [{ "A_one": A_one, "B_one": B_one },
            { "A_other": A_other_one, "B_other": B_other_one }
        ],
        [{ "A_one": A_two, "B_one": B_two },
            { "A_other": A_other_two, "B_other": B_other_two }
        ]
    ]
    
    14 replies  •  2020-11-19 13:53:13 +08:00
    paddistone
    paddistone
    Nov 13, 2020
    1
    k 要是没有意义干嘛不考虑元组;
    需求如此的话,这就是最好的实现了,keep simple, keep stupid ;
    一点要花里胡哨一点,exec 、eval 了解一下,不建议
    RRRoger
    RRRoger
    Nov 13, 2020
    2
    你至少得先把这些重复的东西 提取出一个函数吧
    chaleaoch
    chaleaoch
    Nov 13, 2020
    3
    你的意思 A 和 B 会有很多 成对出现吗? 否则你这个代码看不懂.
    keenkiller
    keenkiller
    Nov 13, 2020
    4
    你这是为了解包?你直接写个数组按索引取不就完了
    xchaoinfo
    xchaoinfo
    Nov 13, 2020
    5
    试试这个模块
    ```python
    from collections import namedtuple
    ```
    MaxTan
    MaxTan
    Nov 13, 2020
    6
    没看懂为啥要这样写,先把思路捋清楚
    css3
    css3
    OP
    Nov 13, 2020
    7
    @css3 @paddistone @RRRoger @chaleaoch @tairan2006 @xchaoinfo @MaxTan

    就是通过 A, B... 生成 all 这个 list, all 里边嵌套 list, 里边又是个 dict, dict 的 key 是固定的
    css3
    css3
    OP
    Nov 13, 2020
    8
    value 根据 A, B unpack 后的值,塞进来
    keenkiller
    keenkiller
    Nov 13, 2020
    9
    就是按索引处理啊…all = [[] for _ in range(10)],然后遍历 A,把 A 的元素先塞到结果数组里,遍历 B 接着塞
    acmore
    acmore
    Nov 13, 2020
    10
    `namedtuple` or `dataclass`?
    ysc3839
    ysc3839
    Nov 13, 2020
    11
    按照你给的代码中我发现的规律:

    data = {'A': [1,2,3,4], 'B': [5,6,7,8], 'C': [9,0,1,2]}
    all = [
    [{f'{k}_one': v[0] for k, v in data.items()}, {f'{k}_other': v[1] for k, v in data.items()}],
    [{f'{k}_one': v[2] for k, v in data.items()}, {f'{k}_other': v[3] for k, v in data.items()}]
    ]
    imn1
    imn1
    Nov 14, 2020
    12
    既然 key 固定,变化不定的只是值,那很多解决办法
    key 固定的,基本上从 sql 思维考虑就可以了,简单就 namedtuple 、csv,复杂就 pandas.DataFrame,sql 数据库
    根据数据来源采用相应方便的方案,然后导出就可以了,可以用 iter() + append
    mizai
    mizai
    Nov 15, 2020
    13
    用 zip 加推导式很容易就可以简化
    A = [1,2,3,4]
    B = ['a','b','c','d']
    C = [random() for i in range(4)]
    data_array = [A,B,C] # 可以添加任意长度的数据
    keys = ['one','other_one','two','other_two'] # dict 的 key 值固定

    all = [dict(zip(column,data_item)) for data_item in zip(*data_array)]
    SjwNo1
    SjwNo1
    Nov 19, 2020
    14
    zip
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2109 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 41ms · UTC 03:25 · PVG 11:25 · LAX 19:25 · JFK 22:25
    ♥ Do have faith in what you're doing.