我有一个项目,我必须通过sys.stdin获取每个单词的频率数量
我已经获得了这部分。第二部分是获取每个单词的行号,我觉得我已经获得了,但我不能在输出字符串中添加行号,并且我也无法找到一种按字母顺序排序的方法,如果单词具有相同的频率
这是我的代码:
if __name__ == '__main__':
wordCount = defaultdict(list)
words = {}
for i, line in enumerate(sys.stdin.readlines()):
wordCount[line].append(i+1) #add the line number to each element in the line
for word in line.lower().split():
words[word] = words.get(word, 0) + 1
sortedList = sorted(words.items(), key=itemgetter(1), reverse=True)
for word, frequency in sortedList:
print("%d %s" % (frequency, word))," " #
“我正在使用python”#line 2
输出应该是:
2 python 1 2
2 really 1 2
1 am 2
1 cool 1
1 I 2
1 is 1
1 with 2
1 working 2