Key error '0' with dict format

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-20 03:12:28

问题


I'm still a beginner in Python, and I wanted to know why this :

    dict = {}
    dict[0] = '123'
    a = 0
    if dict["{}".format(a)]["{}".format(a)] == '1':
        print('True')

gives me a Key Error '0' but not this :

    dict = {}
    dict[0] = '123'
    if dict[0][0] == '1':
       print('True')

Thanks in advance.


回答1:


You're trying to compare the key 0 with "0". They are different. One is an integer and another is a string.



来源:https://stackoverflow.com/questions/24228161/key-error-0-with-dict-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!