3

Let's say I have:

sentences = ['The girls are gorgeous', 'I'm mexican']

And I want to obtain:

words = ['The','girls','are','gorgeous', 'I'm', 'mexican']

I tried:

words = [w.split(' ') for w in sentences] 

but got not expected result.

Will this work for Counter(words) as I need to obtain the frequency?

1
  • 2
    'I'm mexican' is invalid syntax. use "I'm mexican" instead. Commented Apr 9, 2014 at 6:19

3 Answers 3

6

Try like this

sentences = ["The girls are gorgeous", "I'm mexican"]
words = [word for sentence in sentences for word in sentence.split(' ')]
Sign up to request clarification or add additional context in comments.

Comments

4

Your method didn't work because, split returns a list. So, your code creates a nested list. You need to flatten it to use it with Counter. You can flatten it in so many ways.

from itertools import chain
from collections import Counter
Counter(chain.from_iterable(words))

would have been the best way to flatten the nested list and find the frequency. But you can use a generator expression, like this

sentences = ['The girls are gorgeous', "I'm mexican"]
from collections import Counter
print Counter(item for items in sentences for item in items.split())
# Counter({'mexican': 1, 'girls': 1, 'are': 1, 'gorgeous': 1, "I'm": 1, 'The':1})

This takes each sentence, splits that to get the list of words, iterates those words and flattens the nested structure.

If you want to find top 10 words, then you can use Counter.most_common method, like this

Counter(item for items in sentences for item in items.split()).most_common(10)

5 Comments

What if I want to get the 10 most repeated right away?
@diegoaguilar You can use most_common method like this print Counter(item for items in sentences for item in items.split()).most_common(10)
Nice @thefourtheye You should edit your question so it helps more people in future .. Thanks again deeply
@diegoaguilar You meant the answer, right? ;) I updated it :)
God yeah, answer. I'm soo tired now and get confused
2

Try this:

words = ' '.join(sentences).split()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.