Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define a triple layer dictionary in python?

I want to create the following structure for a dictionary:

{ id1: {id2: {id3: [] }}}

That would be a triple dictionary that will finally point to a list.

I use the following code to initiate it in Python:

for i in range(2160):
    for j in range(2160):
        for k in range(2160):
            subnetwork.update({i: {j: {k: [] }}})

This code takes too much time to execute. It is of Big-O(N^3) complexity.

Are there any ways to speed-up this process? Serializing maybe the data structure and retrieving it from hard drive is faster?

What data structures can achieve similar results? Would a flat dictionary using three-element tuples as keys serve my purpose?

like image 823
drizo Avatar asked Jul 19 '26 19:07

drizo


1 Answers

Do you really need 10 billion entries (2,160 ** 3 == 10,077,696,000) in this structure? It's unlikely that any disk-based solution will be quicker than a memory-based one, but at the same time your program might be exceeding the bounds of real memory, causing "page thrashing" to occur.

Without knowing anything about your intended application it's hard to propose a suitable solution. What is it that you are trying to do?

For example, if you don't need to randomly look up items you might consider a flat dictionary using three-element tuples as keys. But since you don't tell use what you are trying to do anything more would probably be highly speculative.

like image 140
holdenweb Avatar answered Jul 22 '26 09:07

holdenweb



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!