Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
List = dictionary
-
I’m searching for a way to see if the numbers in a list = the numbers in a dictionary key. EX: dictionary = {12345:6, 01928:7} list = [1, 2, 3]. And the output would be like: the list fit in the key 12345.
-
@AZOM not nice, I agree
dict = {'12345':6, '01928':7} list = [1, 2, 3] for k in dict.keys(): ok = True for e in list: if str(e) not in k: ok = False break if ok: print('list in',k) break
-
Better
dict = {'12345':6, '1928':7} lst = [1, 2, 3] for k in dict.keys(): if [x for x in lst if str(x) in k] == lst: print('lst in',k) break
-
dict = {'12345':6, '01928':7} list = [1, 2, 3] set_list = set(list) found = [] for k in dict.keys(): if set([int(aK) for aK in k]) >= set_list: found.append(k) print(found)
-
Or, if readability is not much of a concern:
dict = {'12345':6, '01928':7} list = [1, 2, 3] found = [k for k in dict.keys() if set([int(aK) for aK in k]) >= set(list)] print(found)
-
@pulbrich I like it.
-
You almost never need to use dict.keys().
dct = {"12345": 6, "01928": 7} lst = [1, 2, 3, 12, 34, 234] print("\n".join(key for key in dct if all(str(i) in key for i in lst)))
-
This post is deleted!