在文件中保存类实例的字典?

时间:2016-10-22 14:16:04

标签: python dictionary persistent-storage

考虑以下课程和功能:

class DictEntry:
    term = ""
    docFreq = 0
    termFreq = 0
    postingList = []

    def __init__(self, t, df, tf, pl):
        self.term = t
        self.docFreq = df
        self.termFreq = tf
        self.postingList = pl

def insertDict(docId, token, termFreq, dictionary):
    entry = dictionary.get(token)
    if entry is None:
        postList = []
        entry = DictEntry(token, 0, 0, postList)
        dictionary[token] = entry
    entry.docFreq +=1
    entry.postingList.extend([docId])
    entry.termFreq += termFreq

函数insertDict为密钥创建一个对象,如果它不在字典中。

在main中,我需要将dictionary与键和类实例一起保存到文件中。

0 个答案:

没有答案