Sharad S. Sharad S. - 15 days ago 6
Python Question

I have a list of tuples containing dictionaries. How do I edit my code to find the values in a separate list, inside these dictionaries?

I have a data structure L = [(int, dict{key: values}), (int,dict{key: values})...].

Given an input list [0,1] I want to find any dictionary key where both/all values of the input list [0,1] exist.

Currently, my bug is that if I use input_list = [0,1], the function will return a match where the dictionary values are just [0] and where values are [0,1]. Only this second result is desirable. I feel like this is a minute change, but I can't grasp it. What do I change to make this happen?

Code

#Python3
L = [(0, {0: [0], 1: [0, 1], 2: [0, 2], 3: [0, 3], 4: [0, 4]}), (1, {0: [1, 0], 1: [1], 2: [5, 1, 2,], 3: [1, 3], 4: [1, 4]}), (2, {0: [2, 0], 1: [2, 1], 2: [2], 3: [2, 3], 4: [2, 4]}), (3, {0: [3, 0], 1: [3, 1], 2: [3, 2], 3: [3], 4: [3, 4]}), (4, {0: [4, 0], 1: [4, 1], 2: [4, 2], 3: [4, 3], 4: [4]})]

#input_list = (eval(input('Enter your list: ')))
#input_list = ([0,1])
print('Input: ' + str(input_list))
for tupl in L:
dict_a = (tupl[1])
matching_key = ([key for key, value in dict_a.items() if all(v in input_list for v in value)])
print('Node: ' + str(tupl[0]) + ' Match at key(s): ' + str(matching_key))


Output

L = [(0, {0: [0], 1: [0, 1], 2: [0, 2], 3: [0, 3], 4: [0, 4]}), (1, {0: [1, 0], 1: [1], 2: [5, 1, 2,], 3: [1, 3], 4: [1, 4]}), (2, {0: [2, 0], 1: [2, 1], 2: [2], 3: [2, 3], 4: [2, 4]}), (3, {0: [3, 0], 1: [3, 1], 2: [3, 2], 3: [3], 4: [3, 4]}), (4, {0: [4, 0], 1: [4, 1], 2: [4, 2], 3: [4, 3], 4: [4]})]

Enter your list: [0,1]
Input: [0, 1]
Node: 0 Match at key(s): [0, 1]
Node: 1 Match at key(s): [0, 1]
Node: 2 Match at key(s): []
Node: 3 Match at key(s): []
Node: 4 Match at key(s): []

Enter your list: [1,5,2]
Input: [1, 5, 2]
Node: 0 Match at key(s): []
Node: 1 Match at key(s): [1, 2]
Node: 2 Match at key(s): [1, 2]
Node: 3 Match at key(s): []
Node: 4 Match at key(s): []


Thank you:)

Answer

You have the code checking if value contains all the items in input_list the wrong way around. all(v in input_list for v in value) checks that all the items in value can be found from input_list. If you change it the other way around it will work as you expected:

all(v in value for v in input_list)

Note that if you would convert input_list to set you could easily check if input_list is a subset of value. That would be easier to understand and more efficient:

L = [(0, {0: [0], 1: [0, 1], 2: [0, 2], 3: [0, 3], 4: [0, 4]}), (1, {0: [1, 0], 1: [1], 2: [5, 1, 2,], 3: [1, 3], 4: [1, 4]}), (2, {0: [2, 0], 1: [2, 1], 2: [2], 3: [2, 3], 4: [2, 4]}), (3, {0: [3, 0], 1: [3, 1], 2: [3, 2], 3: [3], 4: [3, 4]}), (4, {0: [4, 0], 1: [4, 1], 2: [4, 2], 3: [4, 3], 4: [4]})]

input_list = set([0,1])

for tupl in L:
    dict_a = tupl[1]
    matching_key = [key for key, value in dict_a.items() if input_list <= set(value)]
    print('Node: ' + str(tupl[0]) + ' Match at key(s): ' + str(matching_key))

Output:

Node: 0 Match at key(s): [1]
Node: 1 Match at key(s): [0]
Node: 2 Match at key(s): []
Node: 3 Match at key(s): []
Node: 4 Match at key(s): []