我有一个包含字典的元组列表。如何在这些词典中编辑我的代码以在单独的列表中查找值?

时间:2016-11-22 03:10:30

标签: python dictionary tuples

我有一个数据结构L = [(int,dict {key:values}),(int,dict {key:values})...]。

给定输入列表[0,1]我想找到 任何 字典键,其中输入列表[0,1]的两个/所有值都存在。< / p>

目前,我的错误是如果我使用input_list = [0,1],该函数将返回一个匹配,其中字典值只是[0],其中值是[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): []

谢谢:)

3 个答案:

答案 0 :(得分:2)

如果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_listvalue的子集。这将更容易理解和更有效:

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): []

答案 1 :(得分:0)

您可以使用set subtraction来解决:

#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)]这样的结构,因为它会让你的代码难以理解。

答案 2 :(得分:-1)

请阅读:

Check if two unordered lists are equalHow 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))