V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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 · 2020-11-13 16:29:25 +08:00 · 2416 次点击
    这是一个创建于 1253 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

    # 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 条回复    2020-11-19 13:53:13 +08:00
    paddistone
        1
    paddistone  
       2020-11-13 16:35:28 +08:00
    k 要是没有意义干嘛不考虑元组;
    需求如此的话,这就是最好的实现了,keep simple, keep stupid ;
    一点要花里胡哨一点,exec 、eval 了解一下,不建议
    RRRoger
        2
    RRRoger  
       2020-11-13 16:38:03 +08:00
    你至少得先把这些重复的东西 提取出一个函数吧
    chaleaoch
        3
    chaleaoch  
       2020-11-13 16:38:38 +08:00
    你的意思 A 和 B 会有很多 成对出现吗? 否则你这个代码看不懂.
    tairan2006
        4
    tairan2006  
       2020-11-13 16:40:24 +08:00
    你这是为了解包?你直接写个数组按索引取不就完了
    xchaoinfo
        5
    xchaoinfo  
       2020-11-13 16:41:19 +08:00
    试试这个模块
    ```python
    from collections import namedtuple
    ```
    MaxTan
        6
    MaxTan  
       2020-11-13 16:41:51 +08:00
    没看懂为啥要这样写,先把思路捋清楚
    css3
        7
    css3  
    OP
       2020-11-13 16:49:31 +08:00
    @css3 @paddistone @RRRoger @chaleaoch @tairan2006 @xchaoinfo @MaxTan

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

    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
        12
    imn1  
       2020-11-14 14:32:16 +08:00
    既然 key 固定,变化不定的只是值,那很多解决办法
    key 固定的,基本上从 sql 思维考虑就可以了,简单就 namedtuple 、csv,复杂就 pandas.DataFrame,sql 数据库
    根据数据来源采用相应方便的方案,然后导出就可以了,可以用 iter() + append
    mizai
        13
    mizai  
       2020-11-15 23:33:23 +08:00
    用 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
        14
    SjwNo1  
       2020-11-19 13:53:13 +08:00
    zip
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2920 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 07:39 · PVG 15:39 · LAX 00:39 · JFK 03:39
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.