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

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

问题描述

我找到了一个解决方案,但实际上很慢:

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 xrange(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天全站免登陆