推荐学习书目
› 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
KeepingNow
V2EX  ›  Python

想问一下这里的 key = value 是什么原理?

  •  
  •   KeepingNow · Nov 3, 2018 · 5053 views
    This topic created in 2926 days ago, the information mentioned may be changed or developed.

    刚学 python。

    def build_profile(first, last, **user_info):
        profile = {}
        profile['first_name'] = first
        profile['last_name'] = last
        for key, value in user_info.items():
            profile[key] = value   """问题所在行"""
        return profile
    
    
    user_profile = build_profile(
        'albert', 'einstein', location='princeton', field='physics')
    print(user_profile)
    
    
    {'first_name': 'albert', 'last_name': 'einstein', 'location': 'princeton', 'field': 'physics'}
    

    已经测试必须 profile[key] = value 如此才可以

    在问题行中:user_info 的 key 分别是 location 和 field,value 分别是 princeton physics。profile 这个字典的 key 应该是 user_info 遍历出来的 key 才对啊,那么为什么 key 要等于 value ? 我想知道这是什么原理。 谢谢大家。

    Supplement 1  ·  Nov 3, 2018

    问题已结。把 = 当成等于了,看来等于还是不能随便乱说,时间长了自己都以为是等于,而不是赋值了。

    17 replies  •  2018-11-04 08:20:42 +08:00
    SuperMild
    SuperMild
    Nov 3, 2018
    1
    你是不是把 profile[key] = value 与 key = value 搞混了?
    111111111111
    111111111111
    Nov 3, 2018 via Android❤️1
    3
    那个不是等于,是赋值
    Hopetree
    Hopetree
    Nov 3, 2018❤️1
    4
    这是字典的赋值,并不是判断好么
    laike9m
    laike9m
    Nov 3, 2018
    5
    你在说啥?
    jugelizi
    jugelizi
    Nov 3, 2018
    6
    真的看文档很难吗
    都不知道你在说什么
    KeepingNow
    KeepingNow
    OP
    Nov 3, 2018
    7
    @SuperMild 我把等于和赋值搞混了。现在明白了
    KeepingNow
    KeepingNow
    OP
    Nov 3, 2018
    8
    @leyle 谢谢,看了文档。现在明白了
    KeepingNow
    KeepingNow
    OP
    Nov 3, 2018
    9
    @jugelizi 看了。把赋值当成等于了。谢谢。
    KeepingNow
    KeepingNow
    OP
    Nov 3, 2018
    10
    @111111111111 谢谢,这是问题所在。铜币已发。
    KeepingNow
    KeepingNow
    OP
    Nov 3, 2018
    11
    @Hopetree 对,这是赋值,谢谢,我才想明白。铜币已发。
    sola97
    sola97
    Nov 3, 2018
    12
    单个=一般不都是赋值么,真要判断也都是==啊
    ericls
    ericls
    Nov 3, 2018 via iPhone
    13
    程序语言里面的 = 几乎都是 assign 或者 pattern match
    KeepingNow
    KeepingNow
    OP
    Nov 3, 2018
    14
    @sola97 是的,但是我也不知道怎么就以为是“等于”了,可能是没睡醒的原因(万能的借口
    KeepingNow
    KeepingNow
    OP
    Nov 3, 2018
    15
    @ericls 这个就不是很清楚了,懂点皮毛的编程语言的数量少。但是目前来说我遇到的都是赋值这个概念。
    ethusdt
    ethusdt
    Nov 3, 2018
    16
    放下电脑, 喝杯茶, 出去走走吧, 有时候陷入了一种 chaos 当时是很难爬出来的, 过后更没必要觉得自己愚蠢, 因为人总是会犯错误, 这样学到的东西才能更加记忆犹新.
    Mutoo
    Mutoo
    Nov 4, 2018
    17
    这就是为啥有些语言会把赋值与比较区分开来,例如
    pascal 比较用 = 赋值用 :=
    ecma 比例用 == 赋值用 =
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   688 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 42ms · UTC 20:57 · PVG 04:57 · LAX 12:57 · JFK 15:57
    ♥ Do have faith in what you're doing.