There is a key difference between these two strings:
str1 = "[['a','b'],['c','d'],['e'],['f','g']]"
str2 = "[[a,b],[c,d],[e],[f,g]]"
The first string (str1)
is a pure representation of string list of lists which have strings inside lists. But in the second string (str2)
, the list of lists is made up of variables like a, b, c
. These are not strings and will cause error if you try to print the lists of it. Check this:
print([[a,b],[c,d],[e],[f,g]]) # will throw error as undefined variables
The first string (str1)
can be easily convert to list of lists using ast
Check this:
import ast
str1 = "[['a','b'],['c','d'],['e'],['f','g']]"
str_lists = ast.literal_eval(str1)
for lst in str_lists :
print(lst)
#Output
>>> ['a', 'b']
['c', 'd']
['e']
['f', 'g']
In your case
Your case is likely to be the case of second string (str2)
since it will assume the list as a whole string itself.
str2 = "[[a,b],[c,d],[e],[f,g]]"
If you try to use ast in this case, it will throw error as it contains variables inside list instead of strings.
str2 = "[[a,b],[c,d],[e],[f,g]]"
str_lists = ast.literal_eval(str2)
# it throws error
for lst in str_lists:
print(lst)
So, in order to solve this, you need to use the concept of stack for balanced parentheses. Check this
Solution:
def convert_to_list(myStr):
open_list = ["[","{","("]
close_list = ["]","}",")"]
myStr = myStr.replace(',',' ')
stack = []
stack_ind = []
new_list = []
for innd, i in enumerate(myStr):
if i in open_list:
stack.append(i)
stack_ind.append(innd)
elif i in close_list:
pos = close_list.index(i)
if ((len(stack) > 0) and
(open_list[pos] == stack[len(stack)-1])):
nstr = myStr[stack_ind[-1]+1:innd]
new_list.append(nstr.split())
stack.pop()
new_list.remove(new_list[-1])
return new_list
UserInput = input("Enter 2D list = ")
input_list = convert_to_list(UserInput)
for lst in input_list:
print(lst)
#Output
>>> ['a', 'b']
['c', 'd']
['e']
['f', 'g']