我试图将列表“行”中的每个项目与字典“dic”进行比较,然后将行中的项目添加到 dic(如果它不存在),或者将 1 添加到字典中的键值。import stringdef uniques(): file = open("test.txt", "r") dic = {} data = file.read().replace('\n', ' ') strip = removePunctuation(data) lines = strip.split(' ') print(strip) print(lines) for item in lines: #this produces an error of unhashable type: 'list' if lines in dic: dic[item] = dic[item] + 1 else: print("else ran") dic[item] = 1 for item in dic: print(item, dic[item]) print("There are " + str(len(dic)) + " items in the dictionary")def removePunctuation(text): text = text.strip() return text.rstrip(string.punctuation)uniques()
添加回答
举报
0/150
提交
取消
