[英]Checking if a substring exists in a counter dictionary of strings
假設我有一個這樣的collections.Counter
object
c = Counter({('paragraphs', 'are', 'the'): 1,
('are', 'the', 'building'): 1,
('the', 'building', 'blocks'): 1,
('building', 'blocks', 'of'): 1,
('blocks', 'of', 'papers'): 1,
('many', 'students', 'define'): 1,
('students', 'define', 'paragraphs'): 1,
('define', 'paragraphs', 'in'): 1,
('paragraphs', 'in', 'terms'): 1,
('in', 'terms', 'of'): 1})
現在假設我有一個列表
l = ['many', 'students']
我想檢查l
是否是c
的任何項目的子集。 由於c
有('many', 'students', 'define')
那么根據我的定義l
是c
的子集。
您可以使用set.issubset
操作
l = {'many', 'students'}
for k, v in c.items():
if l.issubset(k):
print(k, v)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.