-2

I have the following list in python

texts = [
    ["great", "even", "for", "the", "non", "runner", "this", "sound",
     "track", "was", "brilliant"],
    ["cannot", "recommend", "as", "a", "former", "comrade", "i", "did",
     "not", "want", "to", "have", "to", "do", "this"]
]

and I want to go through the list and count how often each word appears in it.

I have tried counting the individual words using length() and I get a 2 as a result which means it does not work.

Is there any way I can count how often a word appears in a list as I intend storing the counted word in a new list and it's frequency in another list.

Thanks in advance

Frankie Boyle
  • 29
  • 1
  • 4
  • We won't know how to make the problem go away in your existing code base without seeing your original code. Please post your [mcve](//stackoverflow.com/help/mcve), and fully explain what needs to be modified. Explain the problem, and what output should be produced for this input. – Patrick Artner Jan 06 '19 at 14:14
  • [answer](https://stackoverflow.com/a/5828150/3620003) is in the duplicate. – timgeb Jan 06 '19 at 14:17

4 Answers4

1

First thing to note is probably that texts is a nested list, which is also why you get 2 for len(texts) since texts contains 2 sublists.

If you want to iterate over the individual words, you need to iterate over the sublists and then over the words inside the sublists. Luckily, Python's list comprehensions can be nested:

[word for words in texts for word in words]

As for the counting: The standard library has a dictionary class for exactly such purpose: collections.Counter:

word_counts = collections.Counter(word for words in texts for word in words)

This will give you a dictionary mapping individual words to their occurrence count.

dhke
  • 15,008
  • 2
  • 39
  • 56
0

You can use Counter for this.

texts = [
    ["great", "even", "for", "the", "non", "runner", "this", "sound",
      "track", "was", "brilliant"],
    ["cannot", "recommend", "as", "a", "former", "comrade", "i", "did",
      "not", "want", "to", "have", "to", "do", "this"]
]

for text in texts:
    cnt = Counter()
    for word in text:
        cnt[word] += 1
    print(cnt)
ramazan polat
  • 7,111
  • 1
  • 48
  • 76
0

One liner:

from collections import Counter
from itertools import chain

texts = [["a", "b"], ["a", "c"]]

words_count = Counter(chain(*texts))
print(words_count)

>> Counter({'a': 2, 'b': 1, 'c': 1})
Aaron_ab
  • 3,450
  • 3
  • 28
  • 42
-1

You can count the words with Counter:

from collections import Counter

texts = [["great", "even", "for", "the", "non", "runner", "this", "sound","track", "was", "brilliant"],
         ["cannot", "recommend", "as", "a", "former", "comrade", "i", "did", "not", "want", "to", "have", "to", "do", "this"]]

for text in texts:
    print(Counter(text))

# Counter({'great': 1, 'even': 1, 'for': 1, 'the': 1, 'non': 1, 'runner': 1, 'this': 1, 'sound': 1, 'track': 1, 'was': 1, 'brilliant': 1})
# Counter({'to': 2, 'cannot': 1, 'recommend': 1, 'as': 1, 'a': 1, 'former': 1, 'comrade': 1, 'i': 1, 'did': 1, 'not': 1, 'want': 1, 'have': 1, 'do': 1, 'this': 1})

Source: How do I count unique values inside a list

Darius
  • 10,762
  • 2
  • 29
  • 50