Python - Count number of words in a list strings
Asked Answered
S

9

9

Im trying to find the number of whole words in a list of strings, heres the list

mylist = ["Mahon Point retail park", "Finglas","Blackpool Mahon", "mahon point  blanchardstown"] 

expected outcome:

4
1
2
3

There are 4 words in mylist[0], 1 in mylist[1] and so on

for x, word in enumerate(mylist):
    for i, subwords in enumerate(word):
        print i

Totally doesnt work....

What do you guys think?

Sidewinder answered 16/9, 2013 at 11:45 Comment(1)
possible duplicate of Count word occurrence in a list of stringsDorsum
T
21

Use str.split:

>>> mylist = ["Mahon Point retail park", "Finglas","Blackpool Mahon", "mahon point  blanchardstown"] 
>>> for item in mylist:
...     print len(item.split())
...     
4
1
2
3
Tramontane answered 16/9, 2013 at 11:50 Comment(0)
S
6

The simplest way should be

num_words = [len(sentence.split()) for sentence in mylist]
Sumikosumma answered 16/9, 2013 at 11:50 Comment(0)
N
2

You can use NLTK:

import nltk
mylist = ["Mahon Point retail park", "Finglas","Blackpool Mahon", "mahon point  blanchardstown"]
print(map(len, map(nltk.word_tokenize, mylist)))

Output:

[4, 1, 2, 3]
Naturalistic answered 11/8, 2015 at 23:58 Comment(0)
C
0
for x,word in enumerate(mylist):
    print len(word.split())
Cesura answered 16/9, 2013 at 12:12 Comment(0)
S
0
a="hello world aa aa aa abcd  hello double int float float hello"
words=a.split(" ")
words
dic={}
for word in words:
    if dic.has_key(word):
        dic[word]=dic[word]+1
    else:
        dic[word]=1
dic
Serf answered 13/9, 2016 at 10:30 Comment(2)
Please format you code and explain what additional value your answer brings to other answersCate
If you want to count unique words you can use sets. Simply it is len(set(a.split()))Election
J
0

We can count the number of a word's ocurrence in a list using the Counter function.

from collection import Counter

string = ["mahesh","hello","nepal","nikesh","mahesh","nikesh"]

count_each_word = Counter(string)
print(count_each_word)

Output:

Counter({mahesh:2},{hello:1},{nepal:1},{nikesh:2})

Joost answered 16/5, 2019 at 1:31 Comment(1)
from collections - mind the S and the endRanunculus
M
0

This is another solution:

You can clean your data first and then count the result, something like that:

mylist = ["Mahon Point retail park", "Finglas","Blackpool Mahon", "mahon point  blanchardstown"] 
for item in mylist:
    for char in "-.,":
        item = item.replace(char, '')
        item_word_list = item.split()
    print(len(item_word_list))

The result:

4
1
2
3
Milliner answered 26/8, 2019 at 16:38 Comment(0)
A
0
mylist = ["Mahon Point retail park", "Finglas","Blackpool Mahon", "mahon point blanchardstown"]
flage = True
for string1 in mylist:
    n = 0
    for s in range(len(string1)):
        if string1[s] == ' ' and flage == False:
            n+=1
        if string1[s] == ' ':
            flage = True
        else:
            flage = False
    print(n+1)
Aggregate answered 11/3, 2020 at 19:55 Comment(0)
M
0
lista="Write a Python function to count the number of occurrences of a given characte Write a  of occurrences of a given characte"

dic=lista.split(" ")
wcount={} 
for i in dic:
  if i  in wcount:
    wcount[i]+=1
  else:
    wcount[i]=1 
print(wcount)

picture with solution

Mezzosoprano answered 30/3, 2023 at 10:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.