Python dictionary creation syntax

前端 未结 7 1846
时光取名叫无心
时光取名叫无心 2020-12-30 18:25

I\'m wondering if there\'s any way to populate a dictionary such that you have multiple keys mapping to the same value that\'s less verbose than say:

d = {1:         


        
相关标签:
7条回答
  • 2020-12-30 19:14

    How about:

    501 $ python
    Python 2.7.1+ (r271:86832, Apr 11 2011, 18:13:53) 
    [GCC 4.5.2] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> a = {"q":1}
    >>> print a
    {'q': 1}
    >>> a["q"]
    1
    >>> a["r"] = a["s"] = a["t"] = 2
    >>> a
    {'q': 1, 's': 2, 'r': 2, 't': 2}
    >>> 
    
    0 讨论(0)
提交回复
热议问题