创建一组字典时出现错误。

huangapple go评论71阅读模式
英文:

Error when creating a set of dictionaries

问题

我已经编写了代码来创建一组字典。我期望输出元素以有意义的格式打印出来,但是我遇到了一个错误。

sets_of_dictionary_ele  = [{"name": "Rolf", "grade": "First"},
                           {"name": "Mark", "grade": "Second"},
                           {"name": "John", "grade": "Third"},
                           {"name": "Halen", "grade": "Fourth"}
                          } 

for dictionary in sets_of_dictionary_ele:

    name = dictionary["name"]
    grade = dictionary["grade"]

    print(f"{name} has scored {grade} grade.")

以上是修复后的代码。

英文:

I have written code to create a set of dictionaries. Where I am expecting the output elements to be printed in a meaningful format, however, I am getting an error.

sets_of_dictionary_ele  = {{"name": "Rolf", "grade": "First"},
                           {"name": "Mark", "grade": "Second"},
                           {"name": "John", "grade": "Third"},
                           {"name": "Halen", "grade": "Fourth"}
                           } 

for k,v in sets_of_dictionary_ele:

    name = k["name"]
    grade = v["grade"]

    print(f"{name} has scored {grade} grade.")

答案1

得分: 2

你出现这个错误的原因是因为你使用花括号包装字典,这会尝试创建一个集合。字典是不可哈希的,因此不能作为集合的成员。你可以使用方括号创建字典列表,并相应地修改你的for循环,如下所示:

sets_of_dictionary_ele  = [{"name": "Rolf", "grade": "First"},
                           {"name": "Mark", "grade": "Second"},
                           {"name": "John", "grade": "Third"},
                           {"name": "Halen", "grade": "Fourth"}
                           ]

for k in sets_of_dictionary_ele:
    name = k["name"]
    grade = k["grade"]

    print(f"{name} has scored {grade} grade.")

或者,你可以将你的 sets_of_dictionary_ele 变量的格式修改为字典,并执行以下操作:

sets_of_dictionary_ele = {'Rolf': 'First',
                          'Mark': 'Second',
                          'John': 'Third',
                          'Halen': 'Fourth'}

for k, v in sets_of_dictionary_ele.items():
    name = k
    grade = v
    print(f"{name} has scored {grade} grade.")
英文:

The reason you get this error is because you are wrapping the dicts with curly brackets, which attempts to make a set. Dictionaries are unhashable, so they can't be members of sets. You can either use square brackets to make a list of dicts, and alter your for loop accordingly, as follows:

sets_of_dictionary_ele  = [{"name": "Rolf", "grade": "First"},
                           {"name": "Mark", "grade": "Second"},
                           {"name": "John", "grade": "Third"},
                           {"name": "Halen", "grade": "Fourth"}
                           ]

for k in sets_of_dictionary_ele:
    name = k["name"]
    grade = k["grade"]

    print(f"{name} has scored {grade} grade.")

Or, you can alter the format of your sets_of_dictionary_ele variable to a dictionary and do the following:

sets_of_dictionary_ele = {'Rolf': 'First',
                          'Mark': 'Second',
                          'John': 'Third',
                          'Halen': 'Fourth'}

for k, v in sets_of_dictionary_ele.items():
    name = k
    grade = v
    print(f"{name} has scored {grade} grade.")

huangapple
  • 本文由 发表于 2020年1月3日 20:15:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/59578479.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定