Least common words in a file

paddu

I am interested in finding least common occurring text in a file.

from collections import Counter

# Load the file and extract the words
lines = open("mobydick.txt").readlines()
words = [ word for l in lines for word in l.rstrip().split() ]
print 'No of words in the file:', len(words)

# Use counter to get the counts
counts = Counter( words )

print 'Least common words:'
for word, count in sorted(counts.most_common()[:-3], key=lambda (word, count): (count, word), reverse=True):
    print '%s %s' % (word, count)

How do I limit just 3 words. It prints a bunch.

Andy

You are doing slice over list in a wrong way. Just feel the difference

print [1,2,3,4,5][:-3]
[1, 2]
print [1,2,3,4,5][-3:]
[3, 4, 5]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Find all lines in a text file that have at least two words in common (Bash)

From Dev

Most common words in txt File

From Dev

Perl read a file and an array and find common words

From Dev

Write to file most common words by counter with linux

From Dev

Common words having frequency more than in 2 file python

From Dev

Getting the least amount of sub words

From Dev

Greater Common Divisor/ Least Common Divisor for JavaScript

From Dev

Most common words in string

From Dev

Common words with Javascript

From Dev

Matching strings that contain words with at least 2 spaces

From Dev

At least 3 words or more but in Arabic [HTML Pattern]

From Dev

Regular expression to match at least character and more words

From Dev

Mysql query: Find least used words

From Dev

Check if a string contains at least n words out of a list of words R

From Dev

compare two text files (order does not matter) and output the words the two files have in common to a third file

From Dev

How to write in txt file and find common words in message history from slack in Python?

From Dev

removing least common elements from Counter

From Java

Calculation of Least Common Multiple of the Denominator in Oracle Table

From Dev

What's wrong with this least common ancestor algorithm?

From Dev

Least common TypeTag from two tags in Scala?

From Dev

How to find elements common in at least 2 vectors?

From Dev

Determine least common ancestor at compile-time

From Dev

How to find the least common multiple of a range of numbers?

From Dev

Combine lists that have at least one item in common

From Dev

Least common multiple without using gcd

From Dev

Least common multiple of n numbers, using recursion

From Dev

built-in module to calculate least common multiple

From Dev

How to find elements common in at least 2 vectors?

From Dev

removing least common elements from Counter

Related Related

  1. 1

    Find all lines in a text file that have at least two words in common (Bash)

  2. 2

    Most common words in txt File

  3. 3

    Perl read a file and an array and find common words

  4. 4

    Write to file most common words by counter with linux

  5. 5

    Common words having frequency more than in 2 file python

  6. 6

    Getting the least amount of sub words

  7. 7

    Greater Common Divisor/ Least Common Divisor for JavaScript

  8. 8

    Most common words in string

  9. 9

    Common words with Javascript

  10. 10

    Matching strings that contain words with at least 2 spaces

  11. 11

    At least 3 words or more but in Arabic [HTML Pattern]

  12. 12

    Regular expression to match at least character and more words

  13. 13

    Mysql query: Find least used words

  14. 14

    Check if a string contains at least n words out of a list of words R

  15. 15

    compare two text files (order does not matter) and output the words the two files have in common to a third file

  16. 16

    How to write in txt file and find common words in message history from slack in Python?

  17. 17

    removing least common elements from Counter

  18. 18

    Calculation of Least Common Multiple of the Denominator in Oracle Table

  19. 19

    What's wrong with this least common ancestor algorithm?

  20. 20

    Least common TypeTag from two tags in Scala?

  21. 21

    How to find elements common in at least 2 vectors?

  22. 22

    Determine least common ancestor at compile-time

  23. 23

    How to find the least common multiple of a range of numbers?

  24. 24

    Combine lists that have at least one item in common

  25. 25

    Least common multiple without using gcd

  26. 26

    Least common multiple of n numbers, using recursion

  27. 27

    built-in module to calculate least common multiple

  28. 28

    How to find elements common in at least 2 vectors?

  29. 29

    removing least common elements from Counter

HotTag

Archive