I know very little about programming, so this is a case of not knowing where to look for the answer. I am looking to create a data structure like the following:
vertexTopology = {vertexIndex: {clusterIndexes: intersection point}}
however cluster indexes in reality is a set consisting of the indexes of the clusters. So what I really have now is:
vertexTopology = {5: [[(1, 2, 3), intx_1],
[(2, 3, 4), intx_2]]
6: [[(1, 2, 3), intx_3]]
...}
How can I create a unique index associated to each cluster set AND its vertex index? Something like:
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}]
...}
I'm not sure that what I am looking to do is best achieve with dictionaries, so any suggestion is much welcomed!
Bellow is an image of a four point intersection, just so you can picture a bit what I dealing with.
Data Structures are fundamentals of any programming language around which a program is built. Python helps to learn the fundamental of these data structures in a simpler way as compared to other programming languages.
Python has four non-primitive inbuilt data structures namely Lists, Dictionary, Tuple and Set. These almost cover 80% of the our real world data structures.
There's a thing in Python called a frozen set. That's a set you can use as an index in a dictionary.
vertexTopology = {
5: {
(frozenset({1, 2, 3}), 5): intx_1,
(frozenset({2, 3, 4}), 5): intx_2
},
6: {
(frozenset({1, 2, 3}), 5): intx_3
},
...
}
Unlike sets, frozensets are unmutable. That's why they can be used as an index.
use hash() for generate index for cluster set and vertex index. tuple is hashable type.
vertexTopology = {5: {hash(((1, 2, 3),5)): intx_1,
hash(((2, 3, 4),5)): intx_2},
6: {hash(((1, 2, 3),6)): intx_3},
...}
or use tuple as key
vertexTopology = {5: {((1, 2, 3),5): intx_1,
((2, 3, 4),5): intx_2},
6: {((1, 2, 3),6): intx_3},
...}
if you data use set, tuple() can make tuple from set easily
s = set([1, 2, 3]) # s is set
t = tuple(s) # t is tuple
UPDATE:
if you want other hash method. str() is easy solution.
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'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With