Python dictionary; sum values of the same keys

后端 未结 2 523
情话喂你
情话喂你 2021-01-24 06:50

It might be simple but im just a beginner. If i have this dictionary;

ds = {\"ABab\": 6.25, \"aBab\": 6.25, \"Abab\": 6.25, \"abab\": 6.25, \"ABab\": 6.25, \"aBa         


        
2条回答
  •  灰色年华
    2021-01-24 07:20

    You cannot have the same key twice in python. Every key in the dictionary must be unique. Review the documentation:

    If a key occurs more than once, the last value for that key becomes the corresponding value in the new dictionary.

    Your dictionary is automaticity not regarding the duplicate keys, it will always evaluate the last assigned key.

    duplicate_keys = {"ABab": 6.25, "aBab": 6.25, "Abab": 6.25, "abab": 6.25, "ABab": 6.25, "aBab": 6.25, "Abab": 6.25, "abab": 6.25, "ABab": 6.25, "aBab": 6.25, "Abab": 6.25, "abab": 6.25, "ABab": 6.25, "aBab": 6.25, "Abab": 6.25, "abab": 6.25}
    
    unique_keys = {"ABab": 6.25, "aBab": 6.25, "Abab": 6.25, "abab": 6.25}
    
    duplicate_keys == unique_keys # True
    

提交回复
热议问题