说我有一个唯一的类型A的列表。每个唯一的Type A都有一个子类B的列表,它们也都是唯一的。每个唯一的Type B都有一个唯一的Type C的子类列表。每个唯一类型C都有我要访问的数据。
结构= [A1 [B1 [C1-> $,C2-> $,..],B2 [C1-> $,C2-> $,..] ..],A2 [B1 [C1-> $,C2-> $,..],B2 [C1-> $,C2-> $,..] ...]
我应该如何构造它,以便通过输入structure [A [B [C]]](以A,B和C为该类型的名称)来获取连接到C的数据($)?
答案 0 :(得分:2)
如果您只是对时间复杂度感兴趣,那么词典将是理想的选择。使用字典,您可以使用:
if key in dict:
它是O(n),但是您也可以聪明地使用它:
if dict.get(key):
为O(1)。因此,如果您使用列表字典的字典(听起来像您要找的东西),则唯一依赖于n
的部分是小的数据子列表,并且您的时间复杂度应该很小。但是,如果仍然要遍历计算中的每个A
和B
,那么这根本没有帮助,因为您需要始终查看所有元素(您的用法未在您的任何位置指定问题->为什么如此含糊)。使用这种建议的结构创建数据的示例如下:
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)
可能更进一步地浓缩为字典理解
示例输出(将随机变化):
{'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]}}
此外,这产生了访问my_data["A0"]["B0"][:]