怎么做一个永远记住的列表?

时间:2017-09-05 01:13:23

标签: python list file memory

每次我想要存储以后要记住的信息时,我一直在制作name_of_list.txt文档。但是,我觉得有一种更有效的方法来记住列表而不是扫描文档,看看是否已经有一个单词,如果没有,最后添加它。以下是我通常用于此的代码:

def noun_dict(word):  # Searches to see if the word is already a noun, if not, adds it.
    from string import ascii_letters
    nouns = []
    with open("word_types_nouns.txt", "r") as f:
        lines = f.readlines()
        x = 0
        for line in lines:  # Notice 'words' is different than 'word'
            words = [word for word in line.split() if all(ch in ascii_letters for ch in word)]
            x += 1
            nouns.append(words[0])
        nouns.sort()
        if nouns.count(word) == 0:
            nouns.append(word)
            with open("word_types_nouns.txt", "a") as file:
                file.write("\n" + str(word))
            print(word[0].capitalize() + word[1:], "added as a noun.")
        else:
            print(word[0].capitalize() + word[1:], "already identified as a noun.")
        return nouns

0 个答案:

没有答案