Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python data structure

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.

Four point intersection

like image 706
grasshopper Avatar asked Oct 09 '13 17:10

grasshopper


People also ask

Is Python good for data structures?

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.

What are the 4 built in data structures in Python?

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.


2 Answers

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.

like image 102
Noctua Avatar answered Oct 13 '22 05:10

Noctua


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'
like image 27
Curry Avatar answered Oct 13 '22 06:10

Curry