簡體   English   中英

有效地檢查集合是否具有集合列表中的子集

[英]check if a set has a subset from a list of sets efficiently

我有一個集合列表。

給定一個集合作為輸入,我想檢查集合列表中是否有一個集合是給定集合的子集。

我怎樣才能有效地做到這一點? 我應該使用哪種數據結構?

聽起來很自然你想在這里使用set ,因為它們都是集合 ;-)

這回答了你的問題了嗎:

>>> list_set = [ {1, 2}, {3, 4}, {5, 6}]   # a list of sets
>>> type(list_set[0])
<class 'set'>
>>> given = {5, 6, 7, 2}                   # a given set
>>> for s in list_set:
        if s.issubset(given): print(s)      # print the one found

    
{5, 6}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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