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

2024-06-16 09:44:22 发布

您现在位置:Python中文网/ 问答频道 /正文

假设我有一个大的,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.

Tags: thetoinsendwhichforshelvewe
1条回答
网友
1楼 · 发布于 2024-06-16 09:44:22

在这里,您可以搁置一个简单的字典,并通过键myDict使其易于访问:

import shelve
myDict = {"a" : 1, "b" : 2}
myShelvedDict = shelve.open("my_shelved_dictionary.db")
myShelvedDict["myDict"] = myDict

请注意,字典的内容必须是可腌制的,如任何要搁置的东西。在

如果要复制shelve中字典的结构,即没有myDict键,而是将字典的键直接作为shelve的键,可以使用shelve的update方法:

^{pr2}$

shelve接口与dict接口有很大的重叠。在

相关问题 更多 >