'How I can accept nested dictionary from user in python
I'm working on the simulation of nondeterministic finite automata (NFA), I want to let the user enter the state, alphabet and transaction function.
For transaction functioning: variable t_state the user can enter multiple states for each alphabet like this

I used a nested dictionary
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)
I got this error : dict_name[alphabet] = transiction TypeError: 'str' object does not support item assignment
the expected output:
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']
}
}
Solution 1:[1]
This should do:
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)
Solution 2:[2]
d = {}
size = int(input("Enter the size of nested dictionary: "))
for i in range(size):
d[i+1] = {}
innerkey =list(map(str,input("Enter name of inner dict key: ").split()))
innervalue=list(map(str,input("Enter value of inner dict: ").split()))
d[i+1]["dict_name"] =innerkey
d[i+1]["dict_value"] = innervalue
print(d)
input-Enter the size of nested dictionary: 3
Enter name of inner dict key: a
Enter value of inner dict: 23
Enter name of inner dict key: b
Enter value of inner dict: 45
Enter name of inner dict key: c
Enter value of inner dict: 45
output=
{1: {'dict_name': ['a'], 'dict_value': ['23']}, 2: {'dict_name': ['b'],
'dict_value': ['45']}, 3: {'dict_name': ['c'], 'dict_value': ['45']}}
Solution 3:[3]
This could be a use-case for ast.literal_eval, it's like a "safe eval" to use in this kind of thing.
It's a built-in thing in both Python 3 and Python 2
You could do
from ast import literal_eval
t_state = dict()
s = input('Enter the state for 1: ') # raw_input in Python 2
# the user would enter a Python literal: { 'a': ['2','4'], 'b': ['3'] }
t_state['1'] = literal_eval(s)
print(repr(t_state)) # {'1': {'a': ['2', '4'], 'b': ['3']}}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | SM1312 |
| Solution 2 | zeeshan12396 |
| Solution 3 | mossymountain |
