Python 有三本字典的字典
因此,我设法将3个txt文件转换为一个列表,并将其转换为一个字典,但现在我必须将这3个字典转换为一个具有以下结构的超级字典 “123-45-6789”:{“hw”:[98,89,92,75],“测验”:[45,36,42,50,29,27,40,41],“考试”:[175157]} 我创建了一个默认字典,其中包含所有没有值的studentId 我有三个名为examList、hwList和quizList的字典,它们的结构如下(值的长度不同) {'709-40-8165':[168,98],'560-33-3099':[176,16]} 所以问题是,我如何遍历默认的studentid字典来附加以下字典 下面是一些代码Python 有三本字典的字典,python,dictionary,Python,Dictionary,因此,我设法将3个txt文件转换为一个列表,并将其转换为一个字典,但现在我必须将这3个字典转换为一个具有以下结构的超级字典 “123-45-6789”:{“hw”:[98,89,92,75],“测验”:[45,36,42,50,29,27,40,41],“考试”:[175157]} 我创建了一个默认字典,其中包含所有没有值的studentId 我有三个名为examList、hwList和quizList的字典,它们的结构如下(值的长度不同) {'709-40-8165':[168,98],'56
fullRoster= dict()
idList= []
quizList= []
hwList= []
examList= []
studentids= open("studentids.txt", "r")
idList= [line.rstrip()for line in studentids]
studentids.close()
idList= dict.fromkeys(idList)
#hwFile converted into a list and then into a dictionary
#the exam and homework files follow the same structure
hwFile= open("hwscores.txt", "r")
hwList= [line.rstrip().split() for line in hwFile]
hwFile.close()
#searches for similar ids then places quiz score into single list
for i in range (15):
for k in range ((len(hwList))):
if hwList[i][0]== hwList[k][0] and i!=k:
hwList[i].append((hwList[k][1]))
hwList= hwList[:15]
#adds zero if hw list is not 5
for i in range (15):
if len(hwList[i])!=5:
while len(hwList[i])<5:
hwList[i].append(0)
#dictionary comprehension to create dictionary
hwList= {l[0]: [int(x) for x in l[1:]] for l in hwList}
fullfloster=dict()
idList=[]
quizList=[]
hwList=[]
examList=[]
studentids=open(“studentids.txt”、“r”)
idList=[line.rstrip()用于StudentId中的行]
studentids.close()
idList=dict.fromkeys(idList)
#将文件转换为列表,然后转换为字典
#考试和家庭作业文件遵循相同的结构
hwFile=open(“hwscores.txt”、“r”)
hwList=[line.rstrip().split()表示hwList文件中的行]
hwFile.close()
#搜索相似ID,然后将测验分数放入单个列表中
对于范围(15)内的i:
对于范围内的k((len(hwList)):
如果hwList[i][0]==hwList[k][0]和i=k:
hwList[i].追加((hwList[k][1]))
hwList=hwList[:15]
#如果硬件列表不是5,则添加零
对于范围(15)内的i:
如果len(hwList[i])=5:
而len(hwList[i])我会这样处理它:
# Example exam, hw, and quiz score dicts:
examList = {'709-40-8165': [168, 98], '560-33-3099': [176, 16]}
hwList = {'709-40-8165': [15, 10], '602-33-3099': [17, 5]}
quizList = {'709-40-8165': [15, 10]}
# Add scores for that student ID
def add_scores(superList, scoreList, scoreType):
for studentID,value in scoreList.items():
if studentID not in superList.keys():
superList[studentID] = {}
superList[studentID][scoreType] = value
superList = {}
add_scores(superList, examList, 'exam')
add_scores(superList, quizList, 'quiz')
add_scores(superList, hwList, 'hw')
print(superList)
这使得:
{'602-33-3099': {'hw': [17, 5]}, '709-40-8165': {'exam': [168, 98], 'hw': [15, 10], 'quiz': [15, 10]}, '560-33-3099': {'exam': [176, 16]}}
它迭代每个dict
,并添加该学生ID的分数。下面的代码详细说明了来自的答案
函数将复制学生记录,因此它总是返回传入其中的记录的副本(如果有),否则它将返回一个新字典
import copy
def add_scores(scores, description, records=None):
"""Add scores to student records.
Use this function to add scores for multiple different
kinds of graded exercises in the following way:
>>> exam_scores = {'709-40-8165': [168, 98], '560-33-3099': [176, 16]}
>>> hw_scores = {'709-40-8165': [15, 10], '602-33-3099': [17, 5]}
>>> quiz_scores = {'709-40-8165': [15, 10]}
>>> records = add_scores(exam_scores, 'exam')
>>> records = add_scores(quiz_scores, 'quiz', records)
>>> records = add_scores(hw_scores, 'hw', records)
>>> records == {'560-33-3099': {'exam': [176, 16]}, \
'602-33-3099': {'hw': [17, 5]}, \
'709-40-8165': {'exam': [168, 98], \
'hw': [15, 10], \
'quiz': [15, 10]}}
True
Parameters
----------
scores : dict
each key is a student id
each value is a list of scores
description : str
describes the type of score being added
records : dict
each key is a student id
each value is a dictionary of
{description: scores}
Returns
-------
records : dict
student records in the format described above
"""
records = copy.deepcopy(records) if records else {}
for student_id, student_scores in scores.items():
record = records.setdefault(student_id, {})
record[description] = \
record.get(description, []) + student_scores
return records
如果您可以显示一些代码并引用这些代码来解释您的问题发生了什么,您的问题将得到更好的说明。它还将让读者更快/更好地了解如何帮助您。@idjaw刚刚编辑