如何将值添加到现有字典键 Python

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18817789/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 11:57:34  来源:igfitidea点击:

How to add values to existing dictionary key Python

pythondictionarytuples

提问by OmicronAlpha

I am trying to create a function that takes in four parameters: A keyname, start time, end time, and then a dictionary. I have to create a tuple out of the start time and end time and then append that to a list of tuples, as we will be running this function multiple times. Then I am trying to put certain parts of the list of tuples to certain keynames.

我正在尝试创建一个接受四个参数的函数:键名、开始时间、结束时间和字典。我必须根据开始时间和结束时间创建一个元组,然后将其附加到元组列表中,因为我们将多次运行此函数。然后我试图将元组列表的某些部分放入某些键名。

I think it's better if I would show you would the output looks like:

我认为最好向您展示输出如下所示:

courses = insertIntoDataStruct(“CS 2316”, “1505”, “1555”, courses)  
courses = insertIntoDataStruct(“CS 2316”, “1405”, “1455”, courses)  
courses = insertIntoDataStruct(“CS 2316”, “1305”, “1355”, courses)  
courses = insertIntoDataStruct(“CS 4400”, “1405”, “1455”, courses)  
courses = insertIntoDataStruct(“CS 4400”, “1605”, “1655”, courses)    
print(courses)  


{'CS 2316': [(1505, 1555), (1405, 1455), (1305, 1355)], 'CS 4400': [(1405, 
1455), (1605, 1655)]}

This is my code so far:

到目前为止,这是我的代码:

aDict = {}
tupleList = []
def insertIntoDataStruct(name,startTime,endTime,aDict):       
    timeTuple = tuple([startTime, endTime])  
    tupleList.append(timeTuple)
    aDict[name] = tupleList
    return aDict

I know that this will give me a dictionary with each key having ALL values, but I have no idea on how to make it so that each value that I add will be added on to the dictionary as seen in the output. I have tried a lot of random things, but I am still stuck on this. :(

我知道这会给我一个字典,每个键都有所有值,但我不知道如何制作它,以便我添加的每个值都将添加到字典中,如输出所示。我已经尝试了很多随机的事情,但我仍然坚持这一点。:(

采纳答案by Pooya Eghbali

Try this:

尝试这个:

def insertIntoDataStruct(name,startTime,endTime,aDict):
    if not name in aDict:
        aDict[name] = [(startTime,endTime)]
    else:
        aDict[name].append((startTime,endTime))

Now define your dict:

现在定义你的字典:

courses = {}

and start adding the courses:

并开始添加课程:

insertIntoDataStruct("CS 2316", "1505", "1555", courses)
insertIntoDataStruct("CS 2316", "1405", "1455", courses)
insertIntoDataStruct("CS 2316", "1305", "1355", courses)
insertIntoDataStruct("CS 4400", "1405", "1455", courses)
insertIntoDataStruct("CS 4400", "1605", "1655", courses)

回答by Graeme Stuart

You might want to use a defaultdict instead.

您可能想改用 defaultdict。

from collections import defaultdict

def insertIntoDataStruct(name,startTime,endTime,aDict):
    aDict[name].append((int(startTime), int(endTime)))
    return aDict

courses = defaultdict(list)
courses = insertIntoDataStruct("CS 2316", "1505", "1555", courses)
courses = insertIntoDataStruct("CS 2316", "1405", "1455", courses)
courses = insertIntoDataStruct("CS 2316", "1305", "1355", courses)
courses = insertIntoDataStruct("CS 4400", "1405", "1455", courses)
courses = insertIntoDataStruct("CS 4400", "1605", "1655", courses)


print courses == {'CS 2316': [(1505, 1555), (1405, 1455), (1305, 1355)], 'CS 4400': [(1405, 1455), (1605, 1655)]}
print courses

Perhaps you don't even need a function.

也许您甚至不需要函数。

from collections import defaultdict
courses = defaultdict(list)
courses["CS 2316"].append((int("1505"), int("1555")))
courses["CS 2316"].append((int("1405"), int("1455")))
courses["CS 2316"].append((int("1305"), int("1355")))
courses["CS 4400"].append((int("1405"), int("1455")))
courses["CS 4400"].append((int("1605"), int("1655")))