私信  •  关注

user2263572

user2263572 最近创建的主题
user2263572 最近回复了
7 年前
回复了 user2263572 创建的主题 » python:按键(元组)将字典拆分为更小的字典

当然这可以写得更好,但你应该明白。只需遍历dict,并根据可以动态定义或生成的各种条件检查键的y值。

thing = {
    (1,2): 'a',
    (2,19): 'b'
}

d1 = {}
d2 = {}
for k, v in thing.items():
    // while iterating through the original dict, write some logic to determine how you want to split up based on the y values.
    if k[1] < 5:
        d1[k] = v
    if k[1] < 10:
        d2[k] = v

print(d1, d2)