0

For example, I have a dict below:

a = {'0': {'a', 'c', 'd', 'e', 'b', 'f'},
     '1': {'c', 'b'},
     '2': {'a', 'd', 'e', 'b', 'g'}}

Let's say my input is 'eb'. I want to check which key contains BOTH 'e' and 'b'. How do I make it such that I get back only 0 and 2 as the keys, instead of 1 as well ( because 1 contains b )?

Because I am using " if letter in v", but this only check each letter('e' , then 'b'). How do I check the entire string ( all character) if they are in the value?

Basically how do I split a string but getting individual characters back NOT in a list form

  • FYI for your other question about `AX` sub-strings in `ACCXAAX`, I had a go at it, and found a solution at this link: https://stackoverflow.com/questions/5616822/python-regex-find-all-overlapping-matches. re functions match non-overlapping matches, this post shows how to do overlapping matches. `re.finditer(r'A(?=(\w*)X)',string)` – Nic3500 Aug 18 '20 at 04:15

1 Answers1

0

Using set.issubset:

[k for k, v in a.items() if set("eb").issubset(v)]

Output:

['0', '2']
Chris
  • 29,127
  • 3
  • 28
  • 51