Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用现有词典填充工具架_Python_Mpi_Shelve - Fatal编程技术网

Python 如何使用现有词典填充工具架

Python 如何使用现有词典填充工具架,python,mpi,shelve,Python,Mpi,Shelve,假设我有一个100兆字节的大字典,我想把它放在磁盘架上。我使用pypar来利用MPI生成主列表的清理位。实现这一目标的最佳方式是什么?例如: # much earlier masterDict = shelve.open( 'aShelveFile' ) # .. . . . . # then we work out which parts of masterDict to keep # and we put into aCleanDict # then use mpi pypar to se

假设我有一个100兆字节的大字典,我想把它放在磁盘架上。我使用pypar来利用MPI生成主列表的清理位。实现这一目标的最佳方式是什么?例如:

# much earlier
masterDict = shelve.open( 'aShelveFile' )
# .. . . . . 
# then we work out which parts of masterDict to keep
# and we put into aCleanDict
# then use mpi pypar to send the cleaned bits to the master rank
if pypar.rank() == 0:
  tempdict = {}
  for p in range(1,pypar.size()):
    tempdict.append(pypar.receive(p))
  for l1 in tempdict:
    for l2 in l1:
      realDict.append(l2)
  for p in range(1,pypar.size()):
    pypar.send(realDict,p)

  # now realDict has all the cleaned bits
  # which we send to the other hosts
else:
  pypar.send(aCleanDict, 0 )
  aCleanDict = pypar.receive( 0 )

# now to replace masterDict  with aCleanDict
# note: cannot send shelve dictonaries using pypar

# insert stackover flow code here.

在这里,您可以搁置一本简单的词典,并通过键
myDict
使其易于阅读:

import shelve
myDict = {"a" : 1, "b" : 2}
myShelvedDict = shelve.open("my_shelved_dictionary.db")
myShelvedDict["myDict"] = myDict
请注意,字典的内容必须是可腌制的,就像任何要搁置的内容一样

如果要复制搁置中词典的结构,即没有
myDict
键,但词典的键直接作为搁置的键,可以使用搁置的
update
方法:

myShelvedDict.update(myDict)

shelve
界面与
dict
界面有很大重叠。

仅仅搁置它有问题吗?如何搁置现有词典?我添加了一个简单的示例。不确定这是否是你需要的。如果没有,你可能想澄清一下这个问题。