Reputation: 79
Say I have a list of type A's which are all unique. Each unique Type A has a list of subclass type B's which are also all unique. Each unique Type B has a subclass list of unique type C's. Each unique type C has data that I want to access.
structure = [A1[B1[C1->$, C2->$, ..], B2[C1->$, C2->$, ..] ..], A2[B1[C1->$, C2->$, ..], B2[C1->$, C2->$, ..] ...]
How should I structure this so that by entering structure[A[B[C]]] (with with A, B, and C being the name of that type) I can get the data ($) connected to C?
Upvotes: 0
Views: 35
Reputation: 5774
If you are just interested in the time complexity, then dictionaries would be ideal. With dictionaries you can use:
if key in dict:
which is O(n), but you can also be smart about it and use:
if dict.get(key):
which is O(1). Therefore if you use a dictionary of dictionaries of lists (which sounds like what you are looking for), the only part that depends on n
is the small sublist of data and your time complexity should be rather small. If you are iterating over every A
and B
within a calculation anyways though, then this wouldn't help at all since you need to look at all the elements anyways (your usage is not specified anywhere in your question -> part of why it is so vague). An example of creating data with this suggested structure would look like:
import random
import pprint
my_data = {}
for i in range(5):
a_data = {}
for j in range(5):
a_data.update({"B{}".format(j): [random.random() for _ in range(5)]})
my_data.update({"A{}".format(i): a_data})
pprint.pprint(my_data)
Which could probably be condensed further into a dictionary comprehension
The sample output (will vary randomly):
{'A0': {'B0': [0.6485522824432963,
0.11868349154755786,
0.47993696031349975,
0.7160266451492747,
0.20422803457802052],
'B1': [0.9380849431204785,
0.6539834754308227,
0.5611744002365067,
0.2133551310993106,
0.8949133730542626],
'B2': [0.36760450018709423,
0.9642998035743341,
0.8586193370301718,
0.08752038491887604,
0.024599163698393545],
'B3': [0.6628339212749838,
0.8048498500149749,
0.9907755186375856,
0.6645723436082659,
0.5174314787866218],
'B4': [0.6822316343068052,
0.9746732114075276,
0.0020534953463460237,
0.014356025076888712,
0.6772670570788119]},
'A1': {'B0': [0.7378737756652298,
0.14039653522444384,
0.47866456134664226,
0.10738625746147012,
0.9090969468727612],
'B1': [0.5704370315691024,
0.49376710197367923,
0.8274989999126723,
0.038214369468485554,
0.17009580012847048],
'B2': [0.6939259391972035,
0.9110338881076743,
0.7555660985773318,
0.8481190623903578,
0.014120291785718053],
'B3': [0.8499548396499972,
0.20076400822186358,
0.14614534339302976,
0.7739094289912056,
0.04764385923213177],
'B4': [0.6912718710522611,
0.8048111007740021,
0.44931260952976737,
0.43128050004601304,
0.3531236544706029]},
'A2': {'B0': [0.46528144439859476,
0.0205816059228785,
0.6695456096247111,
0.9988173243509552,
0.051905229109018514],
'B1': [0.04803364032590196,
0.7868149538296291,
0.8533198118962502,
0.29146935162650145,
0.2807743242922375],
'B2': [0.36961149956060024,
0.1353899446333634,
0.7706754890820899,
0.29308242140950314,
0.17034473993878685],
'B3': [0.8354036374070778,
0.646151506493819,
0.32389117378303023,
0.9528267910999805,
0.6267618375506382],
'B4': [0.6145598883893689,
0.8515986400949234,
0.6169785789899879,
0.03362111179099414,
0.9521634858051836]},
'A3': {'B0': [0.9318274094709155,
0.43915402305280726,
0.5606412356801113,
0.5667267202619789,
0.062414540102853966],
'B1': [0.2726691326644528,
0.8541546806395977,
0.39230988690958235,
0.0807340769728665,
0.7751605484452384],
'B2': [0.9397624630926291,
0.9052167655943475,
0.7268766594130203,
0.5576480685216525,
0.07378990948773556],
'B3': [0.15565431133081475,
0.8416385878306458,
0.5316120963188792,
0.9439575177462843,
0.18637945576887305],
'B4': [0.3403489900532247,
0.8965320523548347,
0.2042874328716925,
0.5828873525254279,
0.6355562694141039]},
'A4': {'B0': [0.7249643527845289,
0.06341000284870268,
0.5387352294199541,
0.052360662637426225,
0.32801302810721134],
'B1': [0.7535355207856345,
0.4021768889527224,
0.4538503143135848,
0.9537514506760036,
0.9847844584432128],
'B2': [0.1581364714502792,
0.7496047534745104,
0.8047033267636398,
0.286167263637672,
0.6929734594776367],
'B3': [0.8181548198291808,
0.9249395805906845,
0.12083331687949195,
0.2596558964744917,
0.8351847447381108],
'B4': [0.8998021303404342,
0.12734950119308275,
0.14539257680624873,
0.26646182377533223,
0.7865210152018929]}}
Furthermore, this yields your desired syntax for accessing items (I think...) of my_data["A0"]["B0"][:]
Upvotes: 2