Is there a way to make collections.Counter (Python2.7) aware that its input list is sorted?

后端 未结 3 664
礼貌的吻别
礼貌的吻别 2020-12-16 21:25

The Problem

I\'ve been playing around with different ways (in Python 2.7) to extract a list of (word, frequency) tuples from a corpus, or list of strings, and comp

3条回答
  •  温柔的废话
    2020-12-16 21:43

    Given a sorted list of words as you mention, have you tried the traditional Pythonic approach of itertools.groupby?

    from itertools import groupby
    some_data = ['a', 'a', 'b', 'c', 'c', 'c']
    count = dict( (k, sum(1 for i in v)) for k, v in groupby(some_data) ) # or
    count = {k:sum(1 for i in v) for k, v in groupby(some_data)}
    # {'a': 2, 'c': 3, 'b': 1}
    

提交回复
热议问题