How to add values to existing dictionary key Python

不羁岁月 提交于 2019-12-03 08:27:29

问题


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. :(


回答1:


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)



回答2:


You might want to use a defaultdict instead.

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")))


来源:https://stackoverflow.com/questions/18817789/how-to-add-values-to-existing-dictionary-key-python

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