我有一个数据结构L= (int,dict{key: value}),(int,dict{key: value}).
给定输入列表0,1,我希望找到任何字典键,其中输入列表0,1的两个/所有值都存在。
目前,我的错误是,如果我使用input_list = 0,1,该函数将返回一个匹配,其中字典值正好,值为0,1。只有第二个结果是可取的。我觉得这是一个微小的变化,但我无法理解。我该怎么做才能让这一切发生?
码
#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))
输出
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): []
谢谢您:)
发布于 2016-11-21 19:25:44
您可以使用代码检查value
是否以错误的方式包含input_list
中的所有项。all(v in input_list for v in value)
检查value
中的所有项是否可以从input_list
中找到。如果你用相反的方式改变它,它就会像你预期的那样起作用:
all(v in value for v in input_list)
注意,如果要将input_list
转换为set
,可以很容易地检查input_list
是否是value
的子集。这将更容易理解和更有效率:
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))
输出:
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): []
发布于 2016-11-21 19:31:24
您可以使用set减法来解决:
#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 = [1,5,2]
print('Input: ' + str(input_list))
for tupl in L:
dict_a = (tupl[1])
matching_key = []
for key, lst in tupl[1].items():
if not (set(lst) - set(input_list)):
matching_key.append(key)
print('Node: ' + str(tupl[0]) + ' Match at key(s): ' + str(matching_key))
我建议您避免使用像这个[key for key, value in dict_a.items() if all(v in input_list for v in value)]
这样的构造,因为它会使您的代码难以理解。
发布于 2016-11-21 19:24:17
请阅读:
Check if two unordered lists are equal和How can I compare two ordered lists in python?
根据您在检查是否相等时要使用的语义,您可以使用一种或另一种解决方案。如果您要进行简单的排序列表比较,我将使用以下方法:
for tupl in L:
dict_a = (tupl[1])
if dict_a != input_list:
continue
print('Node: ' + str(tupl[0]) + ' Match at key(s): ' + str(matching_key))
https://stackoverflow.com/questions/40733179
复制相似问题