Python:通过键(元组)将字典拆分成较小的词典 [英] Python: splitting dictionary into smaller dictionaries by keys (tuple)

查看:216
本文介绍了Python:通过键(元组)将字典拆分成较小的词典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一本字典,其中的键是两个整数(x,y)的元组,值是字符串。

I have a dictionary where keys are tuples of two integers (x,y) and values are strings.

如何将字典拆分成较小的字典,其中的拆分取决于 y 值大于某个阈值?

How do I split this dictionary up into smaller dictionaries, where the split is determined by whether the y-value is greater than some treshold?

例如,说我有键(字典值无关紧要,因此在这里省略)

For example, say I have the keys (the dictionary values are irrelevant so I omit them here)

(0,2),(0,4),(0,10),(0,3),(0,11),(0,20),(0 ,8),(0,14)

并说我有阈值 0、5、10、15

然后,一个拆分应该由具有以下键的字典组成:

Then, one split should consist of a dictionary with the following keys:

(0,2),(0,4),(0,3)

由于y值均大于0,但不大于5。

since the y-values are all greater than 0, but not greater than 5.

然后下一个字典应具有键

Then the next dictionary should have keys

( 0,8)

因为它大于0和5,但不大于10。

since it is greater than 0 and 5, but not greater than 10.

然后我们有(0,10),(0,11),(0,14)

因为它大于(或等于)0、5、10,但不等于15。

since it is greater (or equal to) 0, 5, 10, but not 15.

最后,我们有(0 ,20)本身。

推荐答案

您可以使用 collections.defaultdict ,迭代并更新由存储区边界确定的键。与创建可变数量的变量相比,这是个更好的主意。

You can use collections.defaultdict, iterate and update keys determined by the bucket boundaries. This is a better idea than creating a variable number of variables.

d = {(0, 2): 1, (0, 4): 2, (0, 10): 3, (0, 3): 4,
     (0, 11): 5, (0, 20): 6, (0, 8): 7, (0, 14): 8}

L = [0, 5, 10, 15, float('inf')]  # include infinite to facilitate later comparisons

from collections import defaultdict

dd = defaultdict(dict)

for k, v in d.items():
    for i, j in zip(L, L[1:]):
        if i <= k[1] < j:
            dd[i].update({k: v})
            break

print(dd)

defaultdict(dict,
            {0: {(0, 2): 1, (0, 3): 4, (0, 4): 2},
             5: {(0, 8): 7},
             10: {(0, 10): 3, (0, 11): 5, (0, 14): 8},
             15: {(0, 20): 6}})

可以使用 bisect 代替迭代算法来改进算法 L 中的边界。

The algorithm can be improved by using bisect instead of iterating the boundaries in L sequentially.

这篇关于Python:通过键(元组)将字典拆分成较小的词典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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