简体   繁体   English

多层嵌套列表

[英]A multi level nested list

I am trying to implement a customized automata where the transition table looks like: 我正在尝试实现自定义自动机,其中过渡表如下所示:

在此处输入图片说明

The table is dynamic ie the column heading, row name, and data at every cell can be determined at run time. 该表是动态的,即可以在运行时确定每个单元格的列标题,行名和数据。 The column name and row name are also necessary. 列名和行名也是必需的。

I tried this code 我尝试了这段代码

table = []
table.append(["A",[0,["B",2],["C1",2]],[1,["C1",1]]])
table.append(["B",[0,["C",1]],[1,["C2",1]]])
table.append(["C",[0,["C1",1]],[1,["C2",1]]])

but I am unable to access the individual item in the cell ie B or 2 from B:2 etc. Then I tried 但是我无法访问单元格中的单个项目,即B或2从B:2等。然后我尝试了

row = ["A","B","C","C1","C2"]
col = [0,1]
table = [] 
table.append([[["B",2],["C1",2]],["C1",1]])
table.append([["C",1],["C2",1]])
table.append([["C1",1],["C2",1]])

print(table[0][0][0][0])

Now, I can access the individual item (B in the above case) but I am lost with the four subscript. 现在,我可以访问单个项目(在上述情况下为B),但我对四个下标迷失了。 Specially, when I do not know the depth of the list in advance. 特别是当我不知道列表的深度时。 Need to get some help to do it in some easy way. 需要以一些简单的方式获得帮助。 Being a novice, I will appreciate some explanation to the pythonic code. 作为新手,我将感谢您对pythonic代码的一些解释。

Update: This is Non-deterministic Finite Automata. 更新:这是不确定的有限自动机。 I tried the automaton package but they are not solving my problem. 我尝试使用自动机软件包,但是它们不能解决我的问题。 Following the solution of Tadhg-Mcdonald-Jensen, it give the correct out put for the first row (A) in the table but an error message for second row (B). 按照Tadhg-Mcdonald-Jensen的解决方案,它为表中的第一行(A)提供了正确的输出,但为第二行(B)提供了错误消息。 Here is the code 这是代码

table = {}
table["A"] = {0: {"B":2, "C1":2}, 1: {"C1":1}}
table["B"] = {0: {"C":1},         1: {"C2",1}}
table["C"] = {0: {"C1":1},        1: {"C2",1}}

for key,value in table["A"][0].items():  \\ok treated as dictionary (1)
    print(key, value, sep="\t")        
for key,value in table["A"][1].items():  \\ok treated as dictionary (2)
    print(key, value, sep="\t")          
for key,value in table["B"][0].items():  \\ok treated as dictionary (3)
    print(key, value, sep="\t")          
for key,value in table["B"][1].items():  \\wrong: why treated as set? Although same as (2)
    print(key, value, sep="\t")          \\Error message: AttributeError: 'set' object has no attribute 'items' 

The output is 输出是

B   2
C1  2 
C1  1
C   1
Traceback (most recent call last):
  File "C:/Users/Abrar/Google Drive/Tourism Project/Python Projects/nestedLists.py", line 17, in <module>
for key,value in table["B"][1].items():
AttributeError: 'set' object has no attribute 'items'

Pandas is great at doing tables but you could also move to dictionaries, either way, list is not the data structure you want. 熊猫擅长做表,但您也可以使用字典,无论哪种方式,列表都不是您想要的数据结构。

table = {}
table["A"] = {0: {"B":2, "C1":2}, 1: {"C1":1}}
table["B"] = {0: {"C":1},         1: {"C2":1}}
table["C"] = {0: {"C1":1},        1: {"C2":1}}

Then table["A"][0] will give you the first element, each element will have one or more entries, if you wanted to iterate over the entries you can do for key,value in table["A"][0].items() 然后table["A"][0]将为您提供第一个元素,每个元素将具有一个或多个条目,如果要遍历可以for key,value in table["A"][0].items()进行的条目for key,value in table["A"][0].items()

Or to iterate over the entire table you could use 3 nested for loops: 或者要遍历整个表,可以使用3个嵌套的for循环:

#do_stuff = print
for row, line in table.items():
    #each row in the table, row will go through ("A", "B", "C")
    for column, cell in line.items():
        #each cell in the row, column will go through (0, 1)
        for label, value in cell.items(): 
            #each entry in cell, most only have one entry except table["A"][0]
            do_stuff(row, column, label, value)

To be honest I don't understand what the table represents so I can't give you specific advice but I think this would at least be a clearer data structure. 老实说,我不明白表格代表什么,因此我无法为您提供具体建议,但我认为这至少是一个更清晰的数据结构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM