我如何使用散列映射来查询多个参数?

How can i use hashmaps to query for multiple parameters?

如何使用散列-table(可能吗?)来查询属于同一对象的多个参数?

让我解释一下。

如果我有以下对象数组

persons = [{name: "AA"}, {name: "BB"}, {name: "CA"}]

我想将它们存储在散列 table 上,使用 name 作为 value

hashtable.put(persons[0]); // will compute hash("AA")
hashtable.put(persons[1]); // will compute hash("BB")
hashtable.put(persons[2]); // will compute hash("CA")

这将允许我通过 name 非常快速地查询我的散列 table。

我的问题是,是否有任何 hash-table 的实现允许我为更复杂的对象查询多个参数,例如

persons = [{name: "AA", city: "XX"}, {name: "BB", city: "YY"}, {name: "CA", city: "ZZ"}]

例如。寻找 names = "AA"cities = "ZZ"

如果 hash-table 不适用于此类操作,哪种算法或数据结构最适合此类操作?

在 python 中,您可以在哈希图中将 tuples 用作 keys

persons = [{name: "AA", city: "XX"}, {name: "BB", city: "YY"}, {name: "CA", city: "ZZ"}]

hashmap = {}
for d in persons:
    hashmap[(d["name"], d["city"])] = ...

然后你可以像这样查询你的 hashmap:

hashmap[(name, city)]

在其他语言中,您应该能够实现类似的功能(使用元素组作为键)。这可能需要 implementing a custom hash,但哈希映射仍然是正确的数据结构。

哈希表需要完整的键才能被知道。如果一个键由多个字段组成,即使有一个字段是未知的也不起作用。

也许您正在寻找类似分区哈希函数的东西:

http://mlwiki.org/index.php/Partitioned_Hash_Function_Index

用于支持多维索引的关系数据库。