简体   繁体   中英

Efficient way to check if it is a subset of any group in Python

What is an efficient way to iterate subset_candidates and check if it is a subset of any set of groups in Python?
Sample below has only a few items, but I expect 10k of subset_candidates and 10k of groups, so I want to know the efficient way.
Maybe networkX is the solution but I don't know which method should be applied to this case.

subset_candidates = [
  [2, 3], # true (subset of groups[0]) 
  [10, 12], # false
  [100, 110], # true (subset of groups[2])
  [1, 10, 100], # false
]

groups = [
  [1,2,3],
  [10,11,13],
  [100, 105, 110],
]

You can try this using set.issubset

s=map(set,subset_candidates)
g=list(map(set,groups))

for subset in s:
    print(any(subset.issubset(i) for i in g))

Output

True
False
True
False

Convert each list in subset_candidates and each list in groups to set . Then, for each set in subset_candidates check if issubset(a, b)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM