[英]Python data structure design
The data structure should meet the following purpose: 数据结构应满足以下目的:
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. 另一种选择是为您的密钥提出自己的哈希方案(通过将它们包装在类中或仅在字典中使用数字键),但是根据您的访问模式,这可能会更慢。
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.