Python 计算列表中的元组出现次数
Python count tuples occurence in list
有没有办法计算每个元组在这个标记列表中出现了多少次?
我已经尝试了count
方法,但它不起作用。
这是列表:
['hello', 'how', 'are', 'you', 'doing', 'today', 'are', 'you', 'okay']
这些是基于列表的元组:
('hello', 'how')
('how', 'are')
('are','you')
('you', 'doing')
('doing', 'today')
('today', 'are')
('you', 'okay')
我希望结果是这样的
('hello', 'how')1
('how', 'are')1
('are','you')2
('you', 'doing')1
('doing', 'today')1
('today', 'are')1
('you', 'okay')1
您可以轻松地使用 Counter
。计算 n-gram 的通用函数如下:
<b>from collections import Counter</b>
<b>from itertools import islice</b>
def count_ngrams(iterable,n=2):
return <b>Counter</b>(zip(*[<b>islice</b>(iterable,i,None) for i in range(n)]))
这会生成:
>>> count_ngrams(['hello', 'how', 'are', 'you', 'doing', 'today', 'are', 'you', 'okay'],2)
Counter({('are', 'you'): 2, ('doing', 'today'): 1, ('you', 'doing'): 1, ('you', 'okay'): 1, ('today', 'are'): 1, ('how', 'are'): 1, ('hello', 'how'): 1})
>>> count_ngrams(['hello', 'how', 'are', 'you', 'doing', 'today', 'are', 'you', 'okay'],3)
Counter({('are', 'you', 'okay'): 1, ('you', 'doing', 'today'): 1, ('are', 'you', 'doing'): 1, ('today', 'are', 'you'): 1, ('how', 'are', 'you'): 1, ('doing', 'today', 'are'): 1, ('hello', 'how', 'are'): 1})
>>> count_ngrams(['hello', 'how', 'are', 'you', 'doing', 'today', 'are', 'you', 'okay'],4)
Counter({('doing', 'today', 'are', 'you'): 1, ('today', 'are', 'you', 'okay'): 1, ('are', 'you', 'doing', 'today'): 1, ('how', 'are', 'you', 'doing'): 1, ('you', 'doing', 'today', 'are'): 1, ('hello', 'how', 'are', 'you'): 1})
此解决方案需要第三方模块 (iteration_utilities.Iterable
),但应该可以满足您的要求:
>>> from iteration_utilities import Iterable
>>> l = ['hello', 'how', 'are', 'you', 'doing', 'today', 'are', 'you', 'okay']
>>> Iterable(l).successive(2).as_counter()
Counter({('are', 'you'): 2,
('doing', 'today'): 1,
('hello', 'how'): 1,
('how', 'are'): 1,
('today', 'are'): 1,
('you', 'doing'): 1,
('you', 'okay'): 1})
有没有办法计算每个元组在这个标记列表中出现了多少次?
我已经尝试了count
方法,但它不起作用。
这是列表:
['hello', 'how', 'are', 'you', 'doing', 'today', 'are', 'you', 'okay']
这些是基于列表的元组:
('hello', 'how')
('how', 'are')
('are','you')
('you', 'doing')
('doing', 'today')
('today', 'are')
('you', 'okay')
我希望结果是这样的
('hello', 'how')1
('how', 'are')1
('are','you')2
('you', 'doing')1
('doing', 'today')1
('today', 'are')1
('you', 'okay')1
您可以轻松地使用 Counter
。计算 n-gram 的通用函数如下:
<b>from collections import Counter</b>
<b>from itertools import islice</b>
def count_ngrams(iterable,n=2):
return <b>Counter</b>(zip(*[<b>islice</b>(iterable,i,None) for i in range(n)]))
这会生成:
>>> count_ngrams(['hello', 'how', 'are', 'you', 'doing', 'today', 'are', 'you', 'okay'],2)
Counter({('are', 'you'): 2, ('doing', 'today'): 1, ('you', 'doing'): 1, ('you', 'okay'): 1, ('today', 'are'): 1, ('how', 'are'): 1, ('hello', 'how'): 1})
>>> count_ngrams(['hello', 'how', 'are', 'you', 'doing', 'today', 'are', 'you', 'okay'],3)
Counter({('are', 'you', 'okay'): 1, ('you', 'doing', 'today'): 1, ('are', 'you', 'doing'): 1, ('today', 'are', 'you'): 1, ('how', 'are', 'you'): 1, ('doing', 'today', 'are'): 1, ('hello', 'how', 'are'): 1})
>>> count_ngrams(['hello', 'how', 'are', 'you', 'doing', 'today', 'are', 'you', 'okay'],4)
Counter({('doing', 'today', 'are', 'you'): 1, ('today', 'are', 'you', 'okay'): 1, ('are', 'you', 'doing', 'today'): 1, ('how', 'are', 'you', 'doing'): 1, ('you', 'doing', 'today', 'are'): 1, ('hello', 'how', 'are', 'you'): 1})
此解决方案需要第三方模块 (iteration_utilities.Iterable
),但应该可以满足您的要求:
>>> from iteration_utilities import Iterable
>>> l = ['hello', 'how', 'are', 'you', 'doing', 'today', 'are', 'you', 'okay']
>>> Iterable(l).successive(2).as_counter()
Counter({('are', 'you'): 2,
('doing', 'today'): 1,
('hello', 'how'): 1,
('how', 'are'): 1,
('today', 'are'): 1,
('you', 'doing'): 1,
('you', 'okay'): 1})