使用两个键在A中创建词典列表,而不在B中创建词典列表 [英] Use two keys to create a list of dictionaries in A not in B

查看:47
本文介绍了使用两个键在A中创建词典列表,而不在B中创建词典列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是对上一个问题的更复杂的编辑.以前我问过只使用一个键,现在我想学习如何使用两个键:

This is a more complex edit on a previous question. Previously I asked about using only one key, now I would like to learn about how to use two keys:

假设有一个名为list_a的字典列表:

Suppose there is list of dictionaries called list_a:

list_a = [
    {'x' : 1,  'y': 10, 'z': 100},
    {'x' : 1,  'y': 11, 'z': 100},
    {'x' : 1,  'y': 12, 'z': 100},
    {'x' : 2,  'y': 10, 'z': 200},
    {'x' : 2,  'y': 11, 'z': 200},
    {'x' : 2,  'y': 12, 'z': 200}
]

假定存在一个派生列表,该派生列表使用list_a中词典中的键"x".该列表称为list_b:

Suppose there is a derivative list that uses the key 'x' from the dictionaries in list_a. This list will be called list_b:

list_b = [
    {'x' : 1,  'y': 10, 'fruit': 'orange'},
    {'x' : 1,  'y': 12, 'fruit': 'apple'},
    {'x' : 2,  'y': 12, 'fruit': 'banana'}
]

请注意,list_a和list_b的字典没有完全相同的键.唯一等效的键是"x"和"y".否则,这些词典是完全不同的.还请注意,list_b键值相对于原始list_a而言是乱序的.

Notice that list_a and list_b do not have dictionaries full of the same keys. The only equivalent key is 'x' and 'y'. Otherwise, these dictionaries are entirely different. Also notice that the list_b key values are out of order relative to the original list_a.

您将如何生成一个列表,该列表的list_a成员的x,y值不在list_b中?

How would you produce a list with members of list_a with x,y values not in list_b?

例如一个名为list_c的列表:

For example a list called list_c:

list_c = [

    {'x' : 1,  'y': 11, 'z': 100},
    {'x' : 2,  'y': 10, 'z': 200},
    {'x' : 2,  'y': 11, 'z': 200}
]

以前,有人回答说要对一个键"x"执行此操作,这将需要:

Previously, someone answered that to do this on one key 'x', it would take:

list_b_set = {b['x'] for b in list_b}
list_c = [a for a in list_a if a['x'] not in list_b_set]

如何为x和y建立基础?

How can I build on this for 'x' and 'y'?

推荐答案

感谢您鼓励我自己解决此问题的评论.

Thanks to the comments for encouraging me to solve this on my own.

对于那些试图在以下位置查找文档的迷失初学者:

For those lost beginners trying to find documentation on :

[x for x in y if condition]

这称为理解力.我刚开始使用python,却不知道这个东西有一个合适的名字.Google也很难,因为它是由符号和介词组成的.现在您已经知道并可以在理解上查找所有文档!

It's called a comprehension. I just started using python and had no idea this thing had a proper name. It's also difficult to google because it is made of symbols and prepositions. Now you know and can look up all the documentation on comprehensions!

这是解决方案,一旦我查看了理解如何处理两个元素而不是一个元素,这就非常简单

Here is the solution, it was super simple once I looked up how comprehensions can handle two elements instead of one

list_a = [
    {'x' : 1,  'y': 10, 'z': 100},
    {'x' : 1,  'y': 11, 'z': 100},
    {'x' : 1,  'y': 12, 'z': 100},
    {'x' : 2,  'y': 10, 'z': 200},
    {'x' : 2,  'y': 11, 'z': 200},
    {'x' : 2,  'y': 12, 'z': 200}
]

list_b = [
    {'x' : 1,  'y': 10, 'fruit': 'orange'},
    {'x' : 1,  'y': 12, 'fruit': 'apple'},
    {'x' : 2,  'y': 12, 'fruit': 'banana'}
]

list_c = [

    {'x' : 1,  'y': 11, 'z': 100},
    {'x' : 2,  'y': 10, 'z': 200},
    {'x' : 2,  'y': 11, 'z': 200}
]



list_b_set = {(b['x'], b['y']) for b in list_b}
list_c_new = [a for a in list_a if (a['x'],a['y']) not in list_b_set]

list_c_new == list_c

这篇关于使用两个键在A中创建词典列表,而不在B中创建词典列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆