-1

im new to python and trying various libraries

from collections import Counter
print(Counter('like baby baby baby ohhh baby baby like nooo'))

When i print this the output I receive is:

Counter({'b': 10, ' ': 8, 'a': 5, 'y': 5, 'o': 4, 'h': 3, 'l': 2, 'i': 2, 'k': 2, 'e': 2, 'n': 1})

But I want to find the count of unique words:

#output example
({'like': 2, 'baby': 5, 'ohhh': 1, 'nooo': 1}, ('baby', 5))

How can I do this, additionally can I do this without the counter library using loops?

a small orange
  • 560
  • 2
  • 16
  • 1
    Possible duplicate of [Python counter words not letters](https://stackoverflow.com/questions/22563929/python-counter-words-not-letters) – tevemadar May 04 '19 at 22:49
  • 1
    Possible duplicate of [How to find the count of a word in a string?](https://stackoverflow.com/questions/11300383/how-to-find-the-count-of-a-word-in-a-string) – Thierry Lathuille May 04 '19 at 22:51
  • 1
    split text into words and then use list of words with `Counter`. Currently it treads it as list of chars. – furas May 04 '19 at 22:51
  • 1
    Possible duplicate of [Counting unique words in python](https://stackoverflow.com/questions/11899878/counting-unique-words-in-python) – l'L'l May 04 '19 at 22:52
  • yes, you can do it with loop - you will need dictionary to keep results - so finally you build `Counter` – furas May 04 '19 at 22:54
  • 1
    @furas: No loop required... `s = ('like baby baby baby ohhh baby baby like nooo') print(Counter(s.split()))` – l'L'l May 04 '19 at 22:59
  • 1
    @l'L'l OP ask if he can do it with loop and without Counter - so I say he can do it but finally he will build Counter ;) So it makes no sense. – furas May 04 '19 at 23:03

3 Answers3

1

The python Counter class takes an Iterable object as parameter. As you are giving it a String object:

Counter('like baby baby baby ohhh baby baby like nooo')

it will iterate over each character of the string and generate a count for each of the different letters. Thats why you are receiving

Counter({'b': 10, ' ': 8, 'a': 5, 'y': 5, 'o': 4, 'h': 3, 'l': 2, 'i': 2, 'k': 2, 'e': 2, 'n': 1})

back from the class. One alternative would be to pass a list to Counter. This way the Counter class will iterate each of the list elements and create the count you expect.

Counter(['like', 'baby', 'baby', 'baby', 'ohhh', 'baby', 'baby', 'like', 'nooo'])

That could also be simply achived by splitting the string into words using the split method:

Counter('like baby baby baby ohhh baby baby like nooo'.split())

Output

Counter({'baby': 5, 'like': 2, 'ohhh': 1, 'nooo': 1})
melalonso
  • 228
  • 2
  • 9
0

Using the collections.counter you should first split the string into words like so words = 'like baby baby ohhh so forth'.split() Then feed the words variable into the counter.

Yes you can do it without collections module (counter object). There are several ways to do it. One of them, probably not the most efficient one is this:

words = 'like baby baby ohhh so forth'.split()
unique_words = set(words)  # converting to set gets rid of duplicates
wordcount ={}  # an epmty dict
for word in unique_words:
    wordcount[word]=0  # set zero counter for each of the words
for word in words:
    wordcount[word]+= 1  # for each occurrence of a word in the list made fro original string, find that key in dict and increment by 1
print(wordcount)
Ilia Gilmijarow
  • 1,000
  • 9
  • 11
  • Hi sorry for my juvenile question because I am completely new to python, but in your above created script what would I need to print() in order to get my output @ilia –  May 04 '19 at 23:04
  • Oh, I forgot about output, sorry. I corrected the answer to include that – Ilia Gilmijarow May 04 '19 at 23:06
0

Try this:

string = 'like baby baby baby ohhh baby baby like nooo'
words = string.split()
result = dict()

for w in words:
    if result.get(w) == None:
        result[w] = 1
    else:
        result[w] += 1

for w in result:
    print(w + ' -- ' + str(result[w]))
Gianluca Conte
  • 480
  • 4
  • 8