昨天的代码里其实已经有了,一行Line24代码就解决了(Python版的迷你程序——文本内容的简单统计分析),今天来看看字典的使用,注意列表中的元素什么情况下才成为字典的键:
import re import sys filename = sys.argv[1] # 以不区分大小写的单词方式产生一个列表 list_of_words = [] with open(filename, "r") as f: for line in f: list_of_words.extend(re.findall(r"[\w]+", line.lower())) # 以列表的元素值为字典的键,元素的数目为对应键的值 uniquewords = {} for each in list_of_words: if each not in uniquewords: uniquewords[each] = 0 uniquewords[each] += 1 # 找出只出现一次的单词 sone = [] for key, val in uniquewords.items(): if val == 1: sone.append(key) print(sorted(sone))