我正在研究非确定性有限自动机(NFA)的模拟,我想让用户输入状态、字母和事务功能。对于事务功能:变量t_state
用户可以像这样为每个字母输入多个状态
我使用了嵌套字典
count_State = int(input("Please enter the number of NFA states..\n"))
states=list(map(int, input("Enter the states:-").strip().split())) # NFA state set
count_alpha = int(input("Please enter the number of alphabets used\n"))
symbols=list(map(str, input("Enter the NFA alphabets:-").strip().split())) # NFA alphabet
t_state = {}
for i in range(count_State):
dict_name = input("Enter the state: ")
t_state[dict_name] = {}
for i in range(count_alpha):
alphabet = input("Enter the alphabet: ")
numVals = int(input("How many transition for that alphabet?\n"))
transiction = []
for value in range(numVals):
transiction.append(input("Next transition\n"))
dict_name[alphabet] = transiction
print(t_state)
我收到了这个错误:dict_name[alphabet] = transiction TypeError: 'str' object does not support item assignment
预期输出:
t_state = {
'1': {
'a': ['2','4'],
'b': ['3']
},
'2': {
'a': ['5'],
'b': ['1']
},
'3': {
'a': ['5'],
'b': ['1']
},
'4': {
'a': ['1'],
'b': ['5']
},
'5': {
'#': ['2'],
'b': ['3']
}
}
这应该这样做:
count_State = int(input("Please enter the number of NFA states..\n"))
states=list(map(int, input("Enter the states:-").strip().split())) # NFA state set
count_alpha = int(input("Please enter the number of alphabets used\n"))
symbols=list(map(str, input("Enter the NFA alphabets:-").strip().split())) # NFA alphabet
t_state = {}
for i in range(count_State):
dict_name = input("Enter the state: ")
t_state[dict_name] = {}
for i in range(count_alpha):
alphabet = input("Enter the alphabet: ")
numVals = int(input("How many transition for that alphabet?\n"))
transiction = []
for value in range(numVals):
transiction.append(input("Next transition\n"))
t_state[dict_name][alphabet] = transiction
print(t_state)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句