2017-02-19 83 views
0

所以我設法將3個txt文件轉換成一個字典列表,但現在我必須將這3個字典放入具有以下結構的超級字典中。其中有3個字典的字典

「123-45-6789」:{「hw」:[98,89,92,75],「quiz」:[45,36,42,50,29,27,40,41],「考試「:[175157]}

我提出與所有的都沒有的值 的studentids默認字典和我有三個字典稱爲examList,hwList和quizList具有以下結構(值的長度而變化)

{ '709-40-8165':[168,98], '560-33-3099':[176,16]}

所以問題是如何能夠通過默認字典studentid迭代添加以下字典?

下面是一些代碼

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} 
+1

如果您可以顯示一些代碼並引用該代碼來解釋您的問題正在發生什麼,那麼將更好地說明您的問題。它會讓讀者對如何幫助你有更快/更好的理解。 – idjaw

+0

@idjaw剛剛編輯 –

回答

0

我想接近它是這樣的:

# 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的分數。

0

以下代碼詳細說明了Brian的答案。

該函數將複製學生記錄,因此它總是返回傳遞給它的記錄的副本(如果有的話),否則它將返回一個新的詞典。

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