I am trying to implement a customized automata where the transition table looks like:
The table is dynamic i.e. 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 i.e. B or 2 from B:2 etc. Then I tried
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. 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.
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). 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'