简体   繁体   English

如何最佳地存储50万个键值对?

[英]how to optimally store a half million key value pairs?

So, I have an object that has an ID attached to it. 因此,我有一个附加了ID的对象。 I need to do a lookup on that ID and get a different ID. 我需要在该ID上进行查找并获得另一个ID。 I just don't know the most efficient way to do this. 我只是不知道最有效的方法。

Essentially, the transformation will look like this: 本质上,转换将如下所示:

1 -> af567
2 -> at678
3 -> dh675

I will be doing this lookup in Python, and all these values are known before runtime. 我将在Python中进行此查找,并且所有这些值在运行时之前都是已知的。 I thought about Json, SQLite, or a prebuilt dictionary, but I'm just not sure the most optimal route to take. 我想到了Json,SQLite或预建词典,但是我不确定是否要采用最佳途径。 There will be 500,000 of these pairs stored. 这些对将存储500,000。 I just need to know the optimal medium to store these pairs 我只需要知道存储这些对的最佳媒介

Okay so you have 500.000 items where a number maps to a five-digit string: 好的,您有500.000个项目,其中一个数字映射到一个五位数的字符串:

Let's put that to the test 让我们测试一下

def five(i):
    """turns a number into an at least five-digit string"""
    s = hex(s).replace('0x', '')
    return '0' * (5 - len(s)) + s

# 500k items, no duplication
d = {}
for i in range(500000):
    d[i] = five(i)

# see https://code.activestate.com/recipes/577504/
total_size(d, verbose=False) / 1024 / 1024
# => 33.8720645904541 (megabytes)

That's nothing, really. 没什么,真的。 Use a dict. 使用字典。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM