bestehen
V2EX  ›  问与答

Python 字典操作

  •  
  •   bestehen · Aug 2, 2018 · 1529 views
    This topic created in 2844 days ago, the information mentioned may be changed or developed.
    company={}
    news_date="2018-02-23"
    title="asdasdad"
    company[news_date]=title
    company
    {'2018-02-23': 'asdasdad'}
    company.setdefault(news_date,{}).setdefault("asdasasdasdasdqe121")
    Traceback (most recent call last):
    File "<input>", line 1, in <module>
    AttributeError: 'str' object has no attribute 'setdefault'
    company.setdefault(news_date,{"asdasd"})
    'asdasdad'
    company
    {'2018-02-23': 'asdasdad'}
    company.setdefault(news_date,{"asdasdasdada"})
    'asdasdad'

    如何改变 news_date 的 key 为字典类型
    hjq98765
        1
    hjq98765  
       Aug 2, 2018
    可变类型不能作为字典的 key
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3110 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 461ms · UTC 03:52 · PVG 11:52 · LAX 20:52 · JFK 23:52
    ♥ Do have faith in what you're doing.