Python:比较两个dict列表(Python: comparison of two dict lists)

编程入门 行业动态 更新时间:2024-10-26 00:28:57
Python:比较两个dict列表(Python: comparison of two dict lists)

这是我想要实现的目标:

我有两个词典列表。 所有词典都具有以下结构:

dictinary = {'name':'MyName', 'state':'MyState'}

我想浏览两个列表的所有元素,并比较具有相同名称的条目的状态。 这是我能想象到的最佳方式:

for d in list1: name = d['name'] for d2 in list2: if d2['name'] == name: if d1['state'] != d2['state']: # Do something

虽然我认为这种方法可行,但我想知道是否有更高效和/或更优雅的方式来执行此操作。 谢谢你的想法!

Here is what I want to achieve:

I have got two lists of dictionaries. All the dictionaries have the following structure:

dictinary = {'name':'MyName', 'state':'MyState'}

I would like to go through all the elements of both lists and compare the states of the entries with the same name. Here is the best way that I can imagine:

for d in list1: name = d['name'] for d2 in list2: if d2['name'] == name: if d1['state'] != d2['state']: # Do something

While I think that this approach would work, I wonder whether there is a more efficient and/or elegant way to perform this operation. Thank you for your ideas!

最满意答案

看看itertools的product :

import itertools xs = range(1,10) ys = range(11,20) zs = itertools.product(xs,ys) list(zs)

[(1,11),(1,12),(1,13),(1,14),(1,15),(1,16),(1,17),(1,18),( 1,19),(2,11),(2,12),(2,13),(2,14),(2,15),(2,16),(2,17),(2, 18),(2,19),(3,11),(3,12),(3,13),(3,14),(3,15),(3,16),(3,17) ,(3,18),(3,19),(4,11),(4,12),(4,13),(4,14),(4,15),(4,16),( 4,17),(4,18),(4,19),(5,11),(5,12),(5,13),(5,14),(5,15),(5, 16),(5,17),(5,18),(5,19),(6,11),(6,12),(6,13),(6,14),(6,15) ,(6,16),(6,17),(6,18),(6,19),(7,11),(7,12),(7,13),(7,14),( 7,15),(7,16),(7,17),(7,18),(7,19),(8,11),(8,12),(8,13),(8, 14),(8,15),(8,16),(8,17),(8,18),(8,19),(9,11),(9,12),(9,13) ,(9,14),(9,15),(9,16),(9,17),(9,18),(9,19)]

其他几件事 -

当你只代表两件事时,通常使用一个元组(甚至是一个命名的元组),所以要考虑为什么它们是开头的 - 你可能有一个很好的理由:)

[('name','state'),('name','state'),('name','state')...]

另一种方法是直接比较元素,例如你可以检查setA(dicts列表1)和setB(dicts列表2)的交集。

>>> listA = [('fred','A'), ('bob','B'), ('mary', 'D'), ('eve', 'E')] >>> listB = [('fred','X'), ('clive', 'C'), ('mary', 'D'), ('ben','B')] # your listA and listB could be sets to begin with >>> set.intersection(set(listA),set(listB)) set([('mary', 'D')])

然而,这种方法不允许重复......

have a look at product from itertools:

import itertools xs = range(1,10) ys = range(11,20) zs = itertools.product(xs,ys) list(zs)

[(1, 11), (1, 12), (1, 13), (1, 14), (1, 15), (1, 16), (1, 17), (1, 18), (1, 19), (2, 11), (2, 12), (2, 13), (2, 14), (2, 15), (2, 16), (2, 17), (2, 18), (2, 19), (3, 11), (3, 12), (3, 13), (3, 14), (3, 15), (3, 16), (3, 17), (3, 18), (3, 19), (4, 11), (4, 12), (4, 13), (4, 14), (4, 15), (4, 16), (4, 17), (4, 18), (4, 19), (5, 11), (5, 12), (5, 13), (5, 14), (5, 15), (5, 16), (5, 17), (5, 18), (5, 19), (6, 11), (6, 12), (6, 13), (6, 14), (6, 15), (6, 16), (6, 17), (6, 18), (6, 19), (7, 11), (7, 12), (7, 13), (7, 14), (7, 15), (7, 16), (7, 17), (7, 18), (7, 19), (8, 11), (8, 12), (8, 13), (8, 14), (8, 15), (8, 16), (8, 17), (8, 18), (8, 19), (9, 11), (9, 12), (9, 13), (9, 14), (9, 15), (9, 16), (9, 17), (9, 18), (9, 19)]

A couple of other things -

when you are only representing two things, it is common to use a tuple (even a named tuple) so have a think about why they are dicts to begin with - you might have a great reason :)

[('name','state'),('name','state'),('name','state')...]

Another approach, would be to compare elements directly, for example you could check the intersection of setA (list of dicts 1) and setB (list of dicts 2)

>>> listA = [('fred','A'), ('bob','B'), ('mary', 'D'), ('eve', 'E')] >>> listB = [('fred','X'), ('clive', 'C'), ('mary', 'D'), ('ben','B')] # your listA and listB could be sets to begin with >>> set.intersection(set(listA),set(listB)) set([('mary', 'D')])

this approach however does not allow for duplicates...

更多推荐

本文发布于:2023-07-27 15:26:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1292393.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:两个   列表   Python   dict   lists

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!