This may be a duplicate question, but I want to find out is there a way to unpack a list of lists and make a variable from an unpacked result? I have a data in the file like:
'[416213688, 422393399, 190690902, 81688],| [94925847, 61605626, 346027022],| [1035022, 1036527, 1038016]'
So I open a file and make it a list
with open ('data.txt', "r") as f:
a = f.read()
a = a.split("|")
print(*a)
Output:
[416213688, 422393399, 190690902, 81688], [94925847, 61605626, 346027022], [1035022, 1036527, 1038016]
This is the output I need for next step of my program.
But I can't make this result a
variable for using it further. It gives me a SyntaxError: can't use starred expression here
if I try:
a = (*a)
I tried making it by using zip
, but it gives me incorrect output, similar to what is described in the question zip function giving incorrect output.
<zip object at 0x0000000001C86108>
So is there any way to unpack a list of list and get an output like:
[1st list of variables], [2nd list of variables], [etc...]
if i use itertools i get:
l = list(chain(*a))
Out: ['[', '4', '1', '6', '2', '1', '3', '6'...
that is not required
So the working option is https://stackoverflow.com/a/46146432/8589220:
row_strings = a.split(",| ")
grid = [[int(s) for s in row[1:-1].split(", ")] for row in row_strings]
print(",".join(map(str, grid)))
Here's a quick and dirty way to parse the string into a two-dimensional grid (i.e.: a list, which itself contains a list of integers):