简体   繁体   English

Python:无法向字典添加新的key:value

[英]Python: Unable to add a new key:value to a dictionary

I am making a dictionary for storing tests and their grades for different students. 我正在制作一本字典,用于存储针对不同学生的测验及其成绩。

def tests():
    test1 = {'craig':88, 'jeanie':100}
    test2 = {'craig':85, 'jeanie':95}
    test3 = {'craig':80, 'jeanie':98}
    return test1,test2,test3
def actions(test1,test2,test3):
    test1.update({'john':95})
    test1.update({'chris':92})
    test1.update({'charles',100})
    test2.update({'john':100})
    test2.update({'chris':96})
    test2.update({'charles',98})
    test3.update({'john':97})
    test3.update({'chris':100})
    test3.update({'charles',94})
    return test1,test2,test3
def main():
    one,two,three = tests()
    one,two,three = actions(one,two,three)
    print (test1,test2,test3)
main()

However, when I try to append a new key:value to my dicts two errors come up: 但是,当我尝试向我的字典添加新的key:value ,出现两个错误:

First: 第一:

Traceback (most recent call last):
  File "C:\file.py", line 26, in <module>
    main()
  File "C:\file.py", line 24, in main
    one,two,three = actions(one,two,three)
  File "C:\file.py", line 14, in actions
    test1.update({'charles',100})
TypeError: cannot convert dictionary update sequence element #0 to a sequence

Second: 第二:

Traceback (most recent call last):
  File "C:\file.py", line 26, in <module>
    main()
  File "C:\file.py", line 24, in main
    one,two,three = actions(one,two,three)
  File "C:\file.py", line 14, in actions
    test1.update({'charles',100})
ValueError: dictionary update sequence element #0 has length 7; 2 is required

If I run it over and over again, sometimes the first error comes up, sometimes the other. 如果我一遍又一遍地运行它,有时会出现第一个错误,有时会出现另一个错误。

I do not want any imports such as collections . 我不希望任何进口,例如collections

test1.update({'charles',100})

is updating the dict with a set not a dict, which it clearly cannot use to update ... instead of sets pass it dicts 正在使用一组而不是一组dict来更新该dict,它显然不能用于更新...而不是通过set传递它

test1.update({'charles':100})

just to demonstrate 只是为了展示

{1,2,3,4,4,5,6}   # a set that will contain 1,2,3,4,5,6
{1:2,3:4,4:5}   # a dict with 3 elements dict(1=2,3=4,4=5)

If I understand your need you need add new values not update and for that operation you need change update for setdefault method. 如果我了解您的需要,则需要添加新值而不更新,并且对于该操作,您需要更改setdefault方法的更新。 I have tested on Aptana Studio that code: 我已经在Aptana Studio上测试了以下代码:

def tests():
    test1 = {'craig':88, 'jeanie':100}
    test2 = {'craig':85, 'jeanie':95}
    test3 = {'craig':80, 'jeanie':98}
    return test1,test2,test3
def actions(test1,test2,test3):
    test1.setdefault('john',95)
    test1.setdefault('chris',92)
    test1.setdefault('charles',100)
    test2.setdefault('john',100)
    test2.setdefault('chris',96)
    test2.setdefault('charles',98)
    test3.setdefault('john',97)
    test3.setdefault('chris',100)
    test3.setdefault('charles',94)
    return test1,test2,test3
def main():
    one,two,three = tests()
    one,two,three = actions(one,two,three)
    print(one,two,three)
main()

and get response: 并得到回应:

one - {'john': 95, 'charles': 100, 'jeanie': 100, 'chris': 92, 'craig': 88} 
two - {'john': 100, 'charles': 98, 'jeanie': 95, 'chris': 96, 'craig': 85} 
three - {'john': 97, 'charles': 94, 'jeanie': 98, 'chris': 100, 'craig': 80}

Your problem is that update search one dictionary with key for update your value and not insert but setdefault insert new pair key:value with that syntax case not exists and return the value for one key case her exists. 您的问题是,update用键搜索一个字典以更新您的值,而不插入,但setdefault插入新对key:value且该语法不存在,并返回其存在的一个键的值。

Good work for you, 对你来说很好

See answer here: 在这里查看答案:

Add new keys to a dictionary? 向字典添加新键?

To update: 更新:

#### Inserting/Updating value ####
data['a']=1  # updates if 'a' exists, else adds 'a'
# OR #
data.update({'a':1})
# OR #
data.update(dict(a=1))
# OR #
data.update(a=1)
# OR #
data.update([(a,1)])

Instead of this: 代替这个:

test3.update({'charles',94})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Python字典中添加新的键值对 - Add new key value pair in a Python Dictionary 将新的键/值添加到 json 字典 python - add new key/value to json dictionary python Python [嵌套字典] - 如何添加新的键/值来创建这种字典结构:d = {key:{key:value, key:value, key:value}} - Python [Nested Dictionary] - How to add new key/values to create this structure of dictionary: d = {key:{key:value, key:value, key:value}} Python字典添加新键值对的简单方法 - Python dictionary simple way to add a new key value pair 向 Python 中的嵌套字典添加新键值的有效方法? - Efficient way to add a new key-value to a nested dictionary in Python? 如何将新键添加到现有字典并将前一个键作为值附加到 for 循环中创建的新键:python - How to add a new key to an existing dictionary and append previous key as value to the new key created in a for loop : python 在字典中转换2元组键并将其作为字典值添加到新字典中 - Convert 2-tuple key in dictionary and add to a new dictionary as a dictionary value 如何向字典添加新的键值 - How to add new key-value to dictionary 在现有字典中添加一个新键:值并使用 pickle 保存它,然后将该字典加载到另一个 python 文件中 - Add a new key : value inside a existing dictionary and save that using pickle and then load that dictionary in another python file Python:如何遍历词典列表并将每个词典作为值添加到具有唯一键的新词典中-无需重复 - Python: How to iterate over a list of dictionaries and add each dictionary as value to a new dictionary with a unique key - no repeats
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM