簡體   English   中英

Python字典附加鍵:值對

[英]Python dictionary appending key:value pairs

import ast # needed to read text as a dictionary
import operator # needed to find term with maximum value
def define_words():
    final_dict={}
    with open('/Users/admin/Desktop/Dante Dictionary/experimental_dict.txt','r', encoding = "utf-8") as dic:
        dante_dict = ast.literal_eval(dic.read())# reads text as a dictionary
        print('the start length was: ', len(dante_dict)) # start length of source dictionary

        key_to_find = max(dante_dict.items(), key=operator.itemgetter(1))[0]
        print('The next word to define is ', key_to_find) # show which word needs defining

        definition = input('Definition ? : ') # prompt for definition
        for key in dante_dict.keys():
            if key == key_to_find:
                final_dict.update({key_to_find:definition})
        dante_dict.pop(key_to_find)             
        print('the end length is : ' ,len(dante_dict)) 
        print(dante_dict) # print source dictionary, modified
        print(final_dict) # print dictionary with newly defined entry

    with open('/Users/admin/Desktop/Dante Dictionary/experimental_dict.txt', 'w', encoding = 'utf-8') as outfile:
        outfile.write(str(dante_dict)) # writes source dictionary minus newly-defined term

    with open('/Users/admin/Desktop/Dante Dictionary/trial_dictionary.txt', 'w', encoding = 'utf-8') as finalfile:
        finalfile.write(str(final_dict)) 

對於為獲得我的幫助而重新發布類似的問題,我深表歉意。 不知道如何添加修改。 我仍然對此有疑問。 我的最終詞典每次都被覆蓋,而不是附加新定義的術語,因此詞典僅包含最后一個key:value對。我認為,通過使用dict_name [key] = value,將添加新條目,而其他條目保持不變。 感謝幫助

您在每個函數調用上創建一個帶有單個“ key_to_find”鍵的“ final_dict”字典。 我了解(閱讀注釋)您希望函數保留其先前調用的結果,並追加新結果。

當函數返回時,函數的名稱空間會被其中的所有變量破壞。 但是,您可以通過簡單地將代碼重新排列為兩個函數來保存現有字典:

def collectDict():

     # first initialize your final_dict and dante_dict dictionary
     final_dict={}
     with open('/Users/admin/Desktop/Dante Dictionary/experimental_dict.txt','r', encoding = "utf-8") as dic:
             dante_dict = ast.literal_eval(dic.read())# reads text as a dictionary


     # loop as many times you want:
     (dante_dict,final_dict) = define_words(dante_dict,final_dict)    # call the define_words function to update your dictionaries


     # write your dictionaries

     with open('/Users/admin/Desktop/Dante Dictionary/experimental_dict.txt', 'w', encoding = 'utf-8') as outfile:
             outfile.write(str(dante_dict)) # writes source dictionary minus newly-defined term

     with open('/Users/admin/Desktop/Dante Dictionary/trial_dictionary.txt', 'w', encoding = 'utf-8') as finalfile:
             finalfile.write(str(final_dict)) 



 def define_words(dante_dict,final_dict):
      # your already written function without the initialization (first 3 lines) and file writing part

      return(dante_dict,final_dict)  # you return the dictionaries for the other function

這是一個簡單的解決方案,但是請注意, 是完全根據您要嘗試執行的操作而設計的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM