简体   繁体   English

Python数据结构设计

[英]Python data structure design

The data structure should meet the following purpose: 数据结构应满足以下目的:

  • each object is unique with certain key-value pairs 每个对象都是唯一的,具有某些键值对
  • the keys and values are not predetermined and can contain any string value 键和值不是预先确定的,可以包含任何字符串值
  • querying for objects should be fast 查询对象应该很快

Example: 例:

  • object_123({'stupid':True, 'foo':'bar', ...})
  • structure.get({'stupid':True, 'foo':'bar', ...}) should return object_123 structure.get({'stupid':True, 'foo':'bar', ...})应该返回object_123

Optimally this structure is implemented with the standard python data structures available through the standard library. 最好通过标准库中提供的标准python数据结构来实现此结构。

How would you implement this? 您将如何实施?

The simplest solution I can think of is to use sorted tuple keys: 我能想到的最简单的解决方案是使用已排序的元组键:

def key(d): return tuple(sorted(d.items()))

x = {}
x[key({'stupid':True, 'foo':'bar', ...})] = object_123

x.get(key({'stupid':True, 'foo':'bar', ...})) => object_123

Another option would be to come up with your own hashing scheme for your keys (either by wrapping them in a class or just using numeric keys in the dictionary), but depending on your access pattern this may be slower. 另一种选择是为您的密钥提出自己的哈希方案(通过将它们包装在类中或仅在字典中使用数字键),但是根据您的访问模式,这可能会更慢。

I think SQLite or is what you need. 我认为SQLite还是您需要的。 It may not be implemented with standard python structures but it's available through the standard library. 它可能无法通过标准python结构实现,但可以通过标准库获得。

Say object_123 is a dict, which it pretty much looks like. 假设object_123是一个字典,看起来很像。 Your structure seems to be a standard dict with keys like (('foo', 'bar'), ('stupid', True)) ; 您的structure似乎是带有(('foo', 'bar'), ('stupid', True))类的键的标准字典; in other words, tuple(sorted(object_123.items())) so that they're always listed in a defined order. 换句话说,使用tuple(sorted(object_123.items()))以便始终按定义的顺序列出它们。

The reason for the defined ordering is because dict.items() isn't guaranteed to return a list in a given ordering. 定义顺序的原因是因为不能保证dict.items()以给定的顺序返回列表。 If your dictionary key is (('foo', 'bar'), ('stupid', True)) , you don't want a false negative just because you're searching for (('stupid', True),('foo', 'bar')) . 如果您的字典键是(('foo', 'bar'), ('stupid', True)) ,您就不会因为搜索(('stupid', True),('foo', 'bar')) Sorting the values is probably the quickest way to protect against that. 对值进行排序可能是防止这种情况的最快方法。

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

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