English 中文(简体)
• 如何从2份pkl档案/combine 2号nes令中形成一条被nes塞的独裁者?
原标题:How to create a Python nested dictionary from 2 pkl files/combine 2 nested dictionaries into one?

我正试图把两辆被nes塞的暴虐者聚集在一起。 其中每一个国家拥有10个顶级钥匙,而10个钥匙中每个有2个更关键:日期和价值。 在低水平,每个关键词典约有100 000件。

2个字典的原产地为2个 p。 我正在利用负荷功能将这些人分成2个字典。 是否有办法从这2个纸浆档案中找到1个字典? 如果不是的话,我如何能够把2个字典合并成一个字典?

我尝试了。 这一解决办法是,但是它超越了另一个词典,而且我无法得到 还有一名独裁者凌驾于另一人之上。 如果我只向另一个人推荐一名词典,但,那么这个员额似乎表明独裁者不这样做。

因此,我可能认为,我可以在这些独裁者中制造阵列,然后加以改造和加固。 但令人难以置信的是缓慢。 这就是我迄今为止所做的事情:

import cPickle
import numpy as np

def load(filename, verbose=False):
    # Open file
    if verbose : print("Loading %s" % filename)
    pkl_file = open(filename,  rb )
    # Load from Pickle file.
    data = cPickle.load(pkl_file)
    pkl_file.close()

    return data

def combineDicts(dictList):
    result = np.array([])
    for listItem in dictList:
        data = np.array([])
        for item in listItem.keys():
            for innerItem in listItem[item].keys():
                data = np.append(data, listItem[item][innerItem])
        result = np.append(result, data)

因此,我试图领导这些指挥部:

>>> dict1 = load( file1.pkl , verbose = True)
>>> dict2 = load( file2.pkl , verbose = True)
>>> a = combineDicts([dict1, dict2])
最佳回答

如果我正确理解你的问题,我就认为你能够完成你想要的听话(Version 3.x和2.7):

>>> dict1 = { topkey1 : { datetimes : [9,8],  values : [7,6]},  topkey2 : { datetimes : [5,4],  values : [3,2]}}
>>> dict2 = { topkey3 : { datetimes : [9,8],  values : [7,6]},  topkey4 : { datetimes : [5,4],  values : [3,2]}}
>>> dictlist = [dict1, dict2]
>>>  new_dict = {key: value for item in dictlist for key, value in item.items()}
>>> new_dict
{ topkey4 : { values : [3, 2],  datetimes : [5, 4]},  topkey1 : { values : [7, 6],  datetimes : [9, 8]},  topkey3 : { values : [7, 6],  datetimes : [9, 8]},  topkey2 : { values : [3, 2],  datetimes : [5, 4]}}

如果这种结果没有结果,请举例说明最初的字典结构,以及你在字典最后结构中回顾的情况。

Edit:

根据你在评论中提供的情况,应当帮助:

>>> dict1 = { topkey1 : { datetimes : [9,8],  values : [7,6]},  topkey2 : { datetimes : [5,4],  values : [3,2]}}
>>> dict2 = { topkey1 : { datetimes : [29,28],  values : [17,16]},  topkey2 : { datetimes : [35,34],  values : [43,42]}}
>>> for key, value in dict2.items():
...     for subkey, subvalue in value.items():
...         dict1[key][subkey] = dict1[key][subkey] + subvalue
...    
>>> dict1
{ topkey1 : { values : [7, 6, 17, 16],  datetimes : [9, 8, 29, 28]},  topkey2 : { values : [3, 2, 43, 42],  datetimes : [5, 4, 35, 34]}}
问题回答

暂无回答




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签