user5758819
user5758819

Reputation:

Python: making a wordcounter that excludes words less than three letters long

I'm very new to python and I've been trying to make a wordcounter that excludes words less than three letters long. Right now my basic counter looks like this:

wordcount = len(raw_input("Paste your document here: ").split())
print wordcount

This returns the word count but I can't figure out how to make it exclude words with three letters or less. Every time I try something new, I get an iteration error. I've been scouring the internet for some ideas on how to make python recognize how long certain words are, but I haven't had much luck. Any help would be appreciated.

Upvotes: 0

Views: 247

Answers (2)

Scratch'N'Purr
Scratch'N'Purr

Reputation: 10437

You're on the right path. You just split on the input, then using a list comprehension to only select words with len >= 3:

words = raw_input("Paste your document here: ").split()
newwords = [word for word in words if len(word) >= 3
wordcount = len(newwords)

Upvotes: 0

Vedang Mehta
Vedang Mehta

Reputation: 2254

Code -

wordcount = raw_input("Paste your document here: ").split()

wordcount = [word for word in wordcount if len(word) >= 3]

Upvotes: 1

Related Questions