如何用现有词典填充书架
假设我有一本数百兆字节的大字典,我想将其放入磁盘架中。我正在使用 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.
Lets say I have a large, 100s of megabytes, dictionary that I want to make into an on disk shelve. I'm using pypar to utilize MPI to generate cleaned bits of a master list. What's the best way to achieve this? Example:
# 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在这里,您搁置了一个简单的字典,并通过键
myDict
使其可访问:请注意,字典的内容必须是可pickle的,就像要搁置的任何内容一样。
如果您想复制架子中字典的结构,即没有
myDict
键,而是将字典的键直接作为架子的键,则可以使用update shelve 方法:
shelve
接口与dict
接口有很大的重叠。Here you shelve a simple dictionary and make it accessibly via key
myDict
:Note that the contents of the dictionary must be pickleable, as for anything that is to be shelved.
If you want to replicate the structure of the dictionary in the shelve, i.e. not have a
myDict
key but the keys of the dictionary directly as keys of the shelf, you can use theupdate
method of shelve:The
shelve
interface has a large overlap with thedict
one.