如何快速将字典拆分为多个字典 [英] How to split dictionary into multiple dictionaries fast

查看:158
本文介绍了如何快速将字典拆分为多个字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了一个解决方案,但它真的很慢:

I have found a solution but it is really slow:

def chunks(self,data, SIZE=10000):
    for i in xrange(0, len(data), SIZE):
        yield dict(data.items()[i:i+SIZE])

在不使用外部模块(numpy 等)的情况下,您有什么想法吗

Do you have any ideas without using external modules (numpy and etc.)

推荐答案

既然字典这么大,最好把所有涉及的项都保留为迭代器和生成器,就像这样

Since the dictionary is so big, it would be better to keep all the items involved to be just iterators and generators, like this

from itertools import islice

def chunks(data, SIZE=10000):
    it = iter(data)
    for i in range(0, len(data), SIZE):
        yield {k:data[k] for k in islice(it, SIZE)}

示例运行:

for item in chunks({i:i for i in xrange(10)}, 3):
    print(item)

输出

{0: 0, 1: 1, 2: 2}
{3: 3, 4: 4, 5: 5}
{8: 8, 6: 6, 7: 7}
{9: 9}

这篇关于如何快速将字典拆分为多个字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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