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
northisland
V2EX  ›  Python

请分析一下哪个可读性好, a) dict(fast=256*1000, food=3*10) b) dict([ ('fast', 256*1000), ('food', 3*10) ])

  •  
  •   northisland · 2018-12-11 15:47:04 +08:00 · 1652 次点击
    这是一个创建于 1935 天前的主题,其中的信息可能已经有所发展或是发生改变。

    比较菜,今天自己打一遍代码前,不知道 dict 还能做这种类似 **kwargs 的初始化。

    请问 a, b 这 2 种写法,

    • 哪种可读性更好?
    • 哪种是 dict 初始化的 preferably only one ?

    好吧,还是我知识面比较狭窄,确实应该抽空再看看 docs.python.org


    https://docs.python.org/3.1/tutorial/datastructures.html

    The dict() constructor builds dictionaries directly from sequences of key-value pairs:
    
    >>> dict([('sape', 4139), ('guido', 4127), ('jack', 4098)])
    {'sape': 4139, 'jack': 4098, 'guido': 4127}
    In addition, dict comprehensions can be used to create dictionaries from arbitrary key and value expressions:
    
    >>> {x: x**2 for x in (2, 4, 6)}
    {2: 4, 4: 16, 6: 36}
    When the keys are simple strings, it is sometimes easier to specify pairs using keyword arguments:
    
    >>> dict(sape=4139, guido=4127, jack=4098)
    {'sape': 4139, 'jack': 4098, 'guido': 4127}
    
    10 条回复    2018-12-21 17:41:29 +08:00
    holajamc
        1
    holajamc  
       2018-12-11 15:54:33 +08:00   ❤️ 1
    个人喜欢 A 写法~
    northisland
        2
    northisland  
    OP
       2018-12-11 15:55:20 +08:00
    我现在的答案:

    1. 看过教程,觉得都可以了,可读性相同
    2. 这种常见的赋值方法,不存在 preferably only one 方法
    est
        3
    est  
       2018-12-11 16:07:15 +08:00
    用 dict() 生成动态的 。用 { } 定义静态的。
    xpresslink
        4
    xpresslink  
       2018-12-11 16:41:03 +08:00   ❤️ 1
    你这两种写法都不好,如果定义个常量的字典应该直接写成
    d={'sape': 4139, 'jack': 4098, 'guido': 4127}
    这样性能和可读性都好。
    SeaRecluse
        5
    SeaRecluse  
       2018-12-11 16:49:43 +08:00
    d = {}
    d.update{}
    datou
        6
    datou  
       2018-12-11 17:04:41 +08:00
    楼主为啥看 python3.1 的文档?

    应该看 python3.7.1 的文档呀
    northisland
        7
    northisland  
    OP
       2018-12-11 17:34:29 +08:00
    fngtz
        8
    fngtz  
       2018-12-12 04:31:13 +08:00 via iPhone
    第一种直接就生成 dict 对象了。第二种,先生成两个 tuple,再组成一个 list,再 iterate 这个 list 才生成 dict 对象啊。
    northisland
        9
    northisland  
    OP
       2018-12-21 17:39:11 +08:00 via iPad
    @fngtz
    @holajamc

    ```
    key = ‘ 1234 ’
    d = dict(key=key)
    print(d)
    >>> {‘ key ’, ‘ 1234 ’}
    ```
    这句简直是鬼畜啊,dict(key=key)
    holajamc
        10
    holajamc  
       2018-12-21 17:41:29 +08:00
    @northisland 并不会啊,因为第一个 key 是参数名,第二个 key 是参数值呀
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2892 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 14:17 · PVG 22:17 · LAX 07:17 · JFK 10:17
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.