我有一个项目,我必须通过 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))," " # <-- HERE I NEED TO ADD THE LINE NUMBER AND ALSO SORT ALPHABETICALLY
如果我输入“Python 真的很酷”#line 1 “我真的在使用 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