簡體   English   中英

檢查字符串計數器字典中是否存在 substring

[英]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')那么根據我的定義lc的子集。

您可以使用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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM