字典中的Python数组交集 [英] Python intersection of arrays in dictionary
问题描述
我有数组字典,例如:
y_dict= {1: np.array([5, 124, 169, 111, 122, 184]),
2: np.array([1, 2, 3, 4, 5, 6, 111, 184]),
3: np.array([169, 5, 111, 152]),
4: np.array([0, 567, 5, 78, 90, 111]),
5: np.array([]),
6: np.array([])}
我需要在我的字典中找到数组的拦截:y_dict
.
第一步,我从空数组中清除了字典,就像
I need to find interception of arrays in my dictionary: y_dict
.
As a first step I cleared dictionary from empty arrays, as like
dic = {i:j for i,j in y_dict.items() if np.array(j).size != 0}
因此,dic
具有以下视图:
dic = { 1: np.array([5, 124, 169, 111, 122, 184]),
2: np.array([1, 2, 3, 4, 5, 6, 111, 184]),
3: np.array([169, 5, 111, 152]),
4: np.array([0, 567, 5, 78, 90, 111])}
要找到拦截,我尝试使用元组方法,例如:
To find interception I tried to use tuple approach as like:
result_dic = list(set.intersection(*({tuple(p) for p in v} for v in dic.values())))
实际结果为空列表:[]
;
Actual result is empty list: []
;
预期结果应为:[5, 111]
您能帮我在字典中找到数组的交集吗?谢谢
Could you please help me to find intersection of arrays in dictionary? Thanks
推荐答案
您发布的代码过于复杂且错误,因为还需要进行一次额外的内部迭代.您想这样做:
The code you posted is overcomplex and wrong because there's one extra inner iteration that needs to go. You want to do:
result_dic = list(set.intersection(*(set(v) for v in dic.values())))
或带有map
且没有for
循环:
result_dic = list(set.intersection(*(map(set,dic.values()))))
结果
[5, 111]
- 迭代值(忽略键)
- 将每个numpy数组转换为
set
(也可以转换为tuple
,但是intersection
仍会将其转换为集合) - 将参数传递给
intersection
,并将其拆包 - iterate on the values (ignore the keys)
- convert each numpy array to a
set
(converting totuple
also works, butintersection
would convert those to sets anyway) - pass the lot to
intersection
with argument unpacking
我们甚至可以通过在每个数组上创建集合并使用filter
过滤掉空数组来摆脱步骤1:
We can even get rid of step 1 by creating sets on every array and filtering out the empty ones using filter
:
result_dic = list(set.intersection(*(filter(None,map(set,y_dict.values())))))
这是出于单行的缘故,但在现实生活中,表达式可能会被分解,因此更易读&;值得评论.这种分解还可以帮助我们避免在不传递任何参数(因为没有非空集)时发生崩溃,这会挫败与集相交的聪明方法(首先在
That's for the sake of a one-liner, but in real life, expressions may be decomposed so they're more readable & commentable. That decomposition may also help us to avoid the crash which occurs when passed no arguments (because there were no non-empty sets) which defeats the smart way to intersect sets (first described in Best way to find the intersection of multiple sets?).
只需预先创建列表,并且仅当列表不为空时才调用交集.如果为空,则只需创建一个空集即可:
Just create the list beforehand, and call intersection only if the list is not empty. If empty, just create an empty set instead:
non_empty_sets = [set(x) for x in y_dict.values() if x.size]
result_dic = list(set.intersection(*non_empty_sets)) if non_empty_sets else set()
这篇关于字典中的Python数组交集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!