比较2字典最干净的方法是什么? [英] What's the cleanest way to compare 2 dictionary?
问题描述
嗨列表,
我相信有很多方法可以进行比较但是我喜欢看看
如果你有2个字典你会怎么做套装(包含大量的
数据 - 比如20000个密钥,每个密钥包含十几个记录)
你想建立一个关于这两个集的差异列表。
我最终得到3个列表:A中有什么而B中没有,B中有什么
而不是A,当然,A和B都是什么。
您认为最干净的方法是什么? (我相信你会
想出令我惊讶的方式:=))
谢谢,
Hi list,
I am sure there are many ways of doing comparision but I like to see
what you would do if you have 2 dictionary sets (containing lots of
data - like 20000 keys and each key contains a dozen or so of records)
and you want to build a list of differences about these two sets.
I like to end up with 3 lists: what''s in A and not in B, what''s in B
and not in A, and of course, what''s in both A and B.
What do you think is the cleanest way to do it? (I am sure you will
come up with ways that astonishes me :=) )
Thanks,
推荐答案
John Henry写道:
John Henry wrote:
嗨列表,
我相信有很多方法可以进行比较,但我喜欢看看
如果你有2个字典集(包含很多
$ b)你会怎么做$ b数据 - 比如20000个密钥,每个密钥包含十几个记录)
你想建立一个关于这两个集的差异列表。
我最终得到了3个列表:A中有什么而不是B中的内容,B中有什么内容,而不是A中的内容,当然,还有什么内容A和B都是。
您认为最干净的方法是什么? (我相信你会
想出令我惊讶的方式:=))
谢谢,
Hi list,
I am sure there are many ways of doing comparision but I like to see
what you would do if you have 2 dictionary sets (containing lots of
data - like 20000 keys and each key contains a dozen or so of records)
and you want to build a list of differences about these two sets.
I like to end up with 3 lists: what''s in A and not in B, what''s in B
and not in A, and of course, what''s in both A and B.
What do you think is the cleanest way to do it? (I am sure you will
come up with ways that astonishes me :=) )
Thanks,
我赚4个箱子:
a_exclusive_keys
b_exclusive_keys
common_keys_equal_values
common_keys_diff_values >
类似于:
a = {1:1,2:2,3:3,4:4}
b = {2:2,3:-3,5:5}
keya = set(a.keys())
keyb = set(b.keys() )
a_xclusive = keya - keyb
b_xclusive = keyb - keya
_common = keya& keyb
common_eq = set(如果a [k] == b [k],则_com中k为k)
common_neq = _common - common_eq
如果你现在简单设置算术,它应该是OK。
- Paddy。
I make it 4 bins:
a_exclusive_keys
b_exclusive_keys
common_keys_equal_values
common_keys_diff_values
Something like:
a={1:1, 2:2,3:3,4:4}
b = {2:2, 3:-3, 5:5}
keya=set(a.keys())
keyb=set(b.keys())
a_xclusive = keya - keyb
b_xclusive = keyb - keya
_common = keya & keyb
common_eq = set(k for k in _common if a[k] == b[k])
common_neq = _common - common_eq
If you now simple set arithmatic, it should read OK.
- Paddy.
>
Paddy写道:
Paddy wrote:
John Henry写道:
John Henry wrote:
嗨列表,
我相信有很多方法可以进行比较但是我喜欢看看
如果你有2个字典集(包含很多
)你会怎么做
数据 - 比如20000个密钥,每个密钥包含十几个记录)
,你想建立一个关于这两个集的差异列表。
我最终得到3个列表:A中有什么而B中没有,B中有什么
而不是A,当然还有什么''在A和B中都有。
您认为最干净的方法是什么? (我相信你会
想出令我惊讶的方式:=))
谢谢,
Hi list,
I am sure there are many ways of doing comparision but I like to see
what you would do if you have 2 dictionary sets (containing lots of
data - like 20000 keys and each key contains a dozen or so of records)
and you want to build a list of differences about these two sets.
I like to end up with 3 lists: what''s in A and not in B, what''s in B
and not in A, and of course, what''s in both A and B.
What do you think is the cleanest way to do it? (I am sure you will
come up with ways that astonishes me :=) )
Thanks,
我赚4个箱子:
a_exclusive_keys
b_exclusive_keys
common_keys_equal_values
common_keys_diff_values >
类似于:
a = {1:1,2:2,3:3,4:4}
b = {2:2,3:-3,5:5}
keya = set(a.keys())
keyb = set(b.keys() )
a_xclusive = keya - keyb
b_xclusive = keyb - keya
_common = keya& keyb
common_eq = set(如果a [k] == b [k],则_com中k为k)
common_neq = _common - common_eq
如果你现在简单设置算术,它应该是OK。
- Paddy。
I make it 4 bins:
a_exclusive_keys
b_exclusive_keys
common_keys_equal_values
common_keys_diff_values
Something like:
a={1:1, 2:2,3:3,4:4}
b = {2:2, 3:-3, 5:5}
keya=set(a.keys())
keyb=set(b.keys())
a_xclusive = keya - keyb
b_xclusive = keyb - keya
_common = keya & keyb
common_eq = set(k for k in _common if a[k] == b[k])
common_neq = _common - common_eq
If you now simple set arithmatic, it should read OK.
- Paddy.
谢谢,这很干净。给我一个很好的理由升级到Python
2.4。
Thanks, that''s very clean. Give me good reason to move up to Python
2.4.
John Henry写道:
John Henry wrote:
Paddy写道:
Paddy wrote:
John Henry写道:
John Henry wrote:
嗨列表,
>
我相信有很多方法可以做比较但是我喜欢看
是什么如果你有2个字典集(包含很多
数据 - 就像20000个密钥,每个密钥包含十几个记录)你会怎么做?
你想建立关于这两组的差异列表。
>
我最终得到3个列表:A中有什么而B中没有,是什么''在B
而不在A中,当然,A和B都有。
>
做什么你认为这是最干净的方法吗? (我相信你会
想出令我惊讶的方式:=))
>
谢谢,
Hi list,
>
I am sure there are many ways of doing comparision but I like to see
what you would do if you have 2 dictionary sets (containing lots of
data - like 20000 keys and each key contains a dozen or so of records)
and you want to build a list of differences about these two sets.
>
I like to end up with 3 lists: what''s in A and not in B, what''s in B
and not in A, and of course, what''s in both A and B.
>
What do you think is the cleanest way to do it? (I am sure you will
come up with ways that astonishes me :=) )
>
Thanks,
我把它变成4个箱子:
a_exclusive_keys
b_exclusive_keys
common_keys_equal_values
common_keys_diff_values
类似于:
a = {1:1,2:2,3:3,4:4}
b = {2:2,3:-3,5:5}
keya = set(a.keys())
keyb = set (b.keys())
a_xclusive = keya - keyb
b_xclusive = keyb - keya
_common = keya& keyb
common_eq = set(如果a [k] == b [k],则_com中k为k)
common_neq = _common - common_eq
如果你现在简单设置算术,那就应该看好了。
- 帕迪。
I make it 4 bins:
a_exclusive_keys
b_exclusive_keys
common_keys_equal_values
common_keys_diff_values
Something like:
a={1:1, 2:2,3:3,4:4}
b = {2:2, 3:-3, 5:5}
keya=set(a.keys())
keyb=set(b.keys())
a_xclusive = keya - keyb
b_xclusive = keyb - keya
_common = keya & keyb
common_eq = set(k for k in _common if a[k] == b[k])
common_neq = _common - common_eq
If you now simple set arithmatic, it should read OK.
- Paddy.
谢谢,这很干净。给我一个很好的理由升级到Python
2.4。
Thanks, that''s very clean. Give me good reason to move up to Python
2.4.
哦,等等,也适用于2.3。
只需要:
from sets import设置
Oh, wait, works in 2.3 too.
Just have to:
from sets import Set as set
这篇关于比较2字典最干净的方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!