简体   繁体   中英

Sort a text file into a list determined by mode

I have a text file:

hello my name is bill    hello there    hello there    hiya    hiya    hiya

Each phrase is separated by four spaces. How can I order these words (on new lines) by frequency.

Any help appreciated.

You can use collections.Counter for this.

from collections import Counter
with open("your file.txt", "r") as f:
    phrases = Counter(f.read().split("    "))

for phrase, occurrences in sorted(phrases.items(), key=lambda _: _[1], reverse=True):
    print "Phrase: {} -- Occurrences: {}".format(phrase, occurrences)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM