我对编程知之甚少,所以这是一个不知道到哪里去寻找答案的情况。我希望创建如下所示的数据结构:
vertexTopology = {vertexIndex: {clusterIndexes: intersection point}}
然而,集群索引实际上是由集群的索引组成的集合。所以我现在真正拥有的是:
vertexTopology = {5: [[(1, 2, 3), intx_1],
[(2, 3, 4), intx_2]]
6: [[(1, 2, 3), intx_3]]
...}
如何创建与每个簇集及其顶点索引相关联的唯一索引?类似于:
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}]
...}
我不确定我希望做的事情是通过字典来实现的,所以任何建议都是非常受欢迎的!
Bellow是一个四点交叉点的图像,所以你可以想象一下我处理的内容。
发布于 2013-10-10 01:36:17
在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
},
...
}
与集合不同,frozensets是不可变的。这就是为什么它们可以用作索引的原因。
发布于 2013-10-10 01:33:02
使用hash()为簇集和顶点索引生成索引。元组是可哈希类型。
vertexTopology = {5: {hash(((1, 2, 3),5)): intx_1,
hash(((2, 3, 4),5)): intx_2},
6: {hash(((1, 2, 3),6)): intx_3},
...}
或者使用元组作为键
vertexTopology = {5: {((1, 2, 3),5): intx_1,
((2, 3, 4),5): intx_2},
6: {((1, 2, 3),6): intx_3},
...}
如果您数据使用set,tuple()可以很容易地从set转换为tuple
s = set([1, 2, 3]) # s is set
t = tuple(s) # t is tuple
更新:
如果您想要其他散列方法。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'
https://stackoverflow.com/questions/19278690
复制相似问题