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

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

  •  
  •   KeepingNow · 2018-11-03 12:02:26 +08:00 · 4069 次点击
    这是一个创建于 1972 天前的主题,其中的信息可能已经有所发展或是发生改变。

    刚学 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 ? 我想知道这是什么原理。 谢谢大家。

    第 1 条附言  ·  2018-11-03 13:25:25 +08:00

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

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