Reputation: 377
I have a question that requires a list comprehension and MUST use the sum() function, it may not be the best way but that is what is asked. Please read the question below:
Question: Write a function word_count(string, word) that uses a list comprehension and the sum() function to count the number the number of times a word appears in a string. Apply this to the dickens string. Hint: the sum() function can be used to add the elements of a list. For instance, sum([1, 2, 3]) would return 6. Is there a problem with certain words? Which ones and why? Try using the strip string method (we'll revisit this later when we talk about regular expressions).
String used:
dickens = """
It was the best of times, it was the worst of times,
it was the age of wisdom, it was the age of foolishness, it was the epoch of belief,
it was the epoch of incredulity, it was the season of Light, it was the season of Darkness,
it was the spring of hope, it was the winter of despair, we had everything before us, we had
nothing before us, we were all going direct to Heaven, we were all going direct the other way -
in short, the period was so far like the present period, that some of its noisiest authorities
insisted on its being received, for good or for evil, in the superlative degree of comparison only.
"""
def word_count(s, w):
return [word for word in s.strip().split() if w == word ]
print(word_count(dickens, "it"))
output= ['it', 'it', 'it', 'it', 'it', 'it', 'it', 'it', 'it']
So basically from here, using the sum function, how can I get the answer to sum all elements to be 9.
def word_count(s, w):
return sum([word for word in s.strip().split() if w == word ])
print(word_count(dickens, "it"))
This does not work for me but must look something like this.
Thanks
Upvotes: 2
Views: 6012
Reputation: 2705
Others like Adriano here gave good answers.
If what you wanted to accomplish was to count the occurrences of 'it', you can use count(substr)
function for strings.
In your case,
print(dickens.lower().count('it')) # prints 13
EDIT: added lower()
, thanks coldspeed!
Upvotes: 2
Reputation: 37227
If you have to use sum()
for counting purposes, try treating every occurrence of the word as 1. Although this is a suboptimal solution, it's probably right under the given requirements.
sum([1 for word in s.strip().split() if w == word ])
^
It is equivalent to:
sum([1, 1, 1, 1, ......])
There are also other forms of (essentially the same) solution:
sum(w == word for word in s.strip().split())
It is interpreted as
sum( (w == word) for word in s.strip().split() )
and boolean values are treated as ones and zeros when added, so you get the number of matching words.
The latter approach is faster than the first because it creates a generator object instead of a real list full of 1's.
Upvotes: 7
Reputation: 1808
Just add 1 to the array if it exists:
def word_count(s, w):
return sum(1 for word in s.strip().split() if w == word)
It is explicit on the question that he can't use len
, he must use sum.
Upvotes: 4
Reputation: 2706
Use the length of the list comprehension.
def word_count(s, w):
return sum([1 for word in s.strip().split() if w == word ])
print(word_count(dickens, "it"))
Upvotes: -4