gra*_*per 12 python data-structures
我对编程知之甚少,所以这是一个不知道在哪里寻找答案的情况.我想创建一个如下所示的数据结构:
vertexTopology = {vertexIndex: {clusterIndexes: intersection point}}
Run Code Online (Sandbox Code Playgroud)
但实际上,集群索引是由集群索引组成的集合.所以我现在真正拥有的是:
vertexTopology = {5: [[(1, 2, 3), intx_1],
[(2, 3, 4), intx_2]]
6: [[(1, 2, 3), intx_3]]
...}
Run Code Online (Sandbox Code Playgroud)
如何创建与每个集群及其顶点索引关联的唯一索引?就像是:
vertexTopology = {5: {index associated with (1, 2, 3) AND vertex 5, intx_1},
{index associated with (2, 3, 4) AND vertex 5, intx_2},
6: {index associated with (1, 2, 3) AND vertex 6, intx_3}]
...}
Run Code Online (Sandbox Code Playgroud)
我不确定我要做的是最好用词典来实现,所以任何建议都受到欢迎!
贝娄是一个四点交叉的图像,所以你可以想象一下我处理的东西.

Python中有一个称为冻结集的东西.这是一个可以用作字典索引的集合.
vertexTopology = {
5: {
(frozenset({1, 2, 3}), 5): intx_1,
(frozenset({2, 3, 4}), 5): intx_2
},
6: {
(frozenset({1, 2, 3}), 5): intx_3
},
...
}
Run Code Online (Sandbox Code Playgroud)
与集合不同,frozensets是不可变的.这就是为什么它们可以用作索引.
使用 hash() 生成簇集索引和顶点索引。tuple 是可哈希类型。
vertexTopology = {5: {hash(((1, 2, 3),5)): intx_1,
hash(((2, 3, 4),5)): intx_2},
6: {hash(((1, 2, 3),6)): intx_3},
...}
Run Code Online (Sandbox Code Playgroud)
或使用元组作为键
vertexTopology = {5: {((1, 2, 3),5): intx_1,
((2, 3, 4),5): intx_2},
6: {((1, 2, 3),6): intx_3},
...}
Run Code Online (Sandbox Code Playgroud)
如果您的数据使用集合, tuple() 可以轻松地从集合中生成元组
s = set([1, 2, 3]) # s is set
t = tuple(s) # t is tuple
Run Code Online (Sandbox Code Playgroud)
更新:
如果你想要其他哈希方法。str() 是一个简单的解决方案。
In [41]: import hashlib
In [42]: hashed = hashlib.sha512(str(((1, 2, 3), 4))).digest()
In [43]: hashed
Out[43]:
'mtE7\xf6N\xfc\xca\xc7\xb1\x0fA\x86|\xbe9j\xbb\xdf\xbaa\xd1\x05V\x84\xe8S\xfb\xe1\x16\xe05\x89,C\xa8\x94n\xae\x1e\n\xc0Y-)\xfa\xceG D\xe0C\xc9\xef\xb0\x8eCk\xe3`\xc2s\x97\xec'
Run Code Online (Sandbox Code Playgroud)