5

I want to know if there is a way to count element frequencies in a 2D python list. For 1D lists, we can use

list.count(word)

but what if I have a list:

a = [ ['hello', 'friends', 'its', 'mrpycharm'], 
      ['mrpycharm', 'it', 'is'], 
      ['its', 'mrpycharm'] ]

can i find the frequency for each word in this 2D list?

0

3 Answers 3

7

Assuming I understand what you want,

>>> collections.Counter([x for sublist in a for x in sublist])
Counter({'mrpycharm': 3, 'its': 2, 'friends': 1, 'is': 1, 'it': 1, 'hello': 1})

Or,

>>> c = collections.Counter()
>>> for sublist in a:
...     c.update(sublist)
...
>>> c
Counter({'mrpycharm': 3, 'its': 2, 'friends': 1, 'is': 1, 'it': 1, 'hello': 1})
Sign up to request clarification or add additional context in comments.

1 Comment

Do you know if using a generator expression instead of a list comprehension would result in a speedup? Counter(x for sublist in a for x in sublist)
4

You can use a defaultdict:

from collections import defaultdict
d = defaultdict(int)
for sublist in a:
    for word in sublist:
        d[word] += 1

6 Comments

@juanpa.arrivillaga why is it a wrong collection?
Look at sberry's answer
but this seems to work fine too
@juanpa.arrivillaga: But that doesn't makes it the wrong collection.
Not the wrong collection, just a different choice. Prior to its introduction in 2.7 this is exactly how I would have done it. For what it's worth, for this size data on my machine (and CPython2.7) this solution is more than 50% faster than using Counter.
|
1

You already know about list.count(). Simply get the count of word in each sublist and sum them. For example:

>>> my_word = 'its'
>>> sum(sublist.count(my_word) for sublist in a)
2

In case you want the frequency of every word present in your list, there are many good answers available here for that. Alternatively, in case you want to do it without any import (using normal dict), you may do:

my_dict = {}
for sublist in a:
    for item in sublist:
        if item not in my_dict:
            my_dict[item] = 0
        my_dict[item] += 1

# Value of my_dict:
{'friends': 1, 'is': 1, 'it': 1, 'its': 2, 'mrpycharm': 3, 'hello': 1}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.