Python 嵌套列表推导来计算列表中相同字符串的出现次数

Python nested list comprehension to count occurrences of same string across lists

我需要转换以下列表:

[
  [u'starred', u'review'],
  [u'starred', u'review'],
  [u'starred', u'review', u'pinned'],
  [u'starred'],
  [u'starred'],
  [u'starred', u'review']
]

进入下面的字典

{
  u'starred': 6,
  u'review': 4,
  u'pinned': 1
}

我感觉应该可以通过使用列表理解或生成器表达式来实现,但我不确定。

有什么想法吗?

怎么样

import itertools, collections
c = collections.Counter(itertools.chain(*your_list))

用生成器表达式展开列表并使用 collections.Counter:

from collections import Counter

Counter(x for sub in L for x in sub)