python - How to unpack a list of lists? -
this may duplicate question, want find out there way unpack list of lists , make variable unpacked result? have data in file like:
'[416213688, 422393399, 190690902, 81688],| [94925847, 61605626, 346027022],| [1035022, 1036527, 1038016]'
so open file , make list
with open ('data.txt', "r") f: = f.read() = a.split("|") print(*a)
output:
[416213688, 422393399, 190690902, 81688], [94925847, 61605626, 346027022], [1035022, 1036527, 1038016]
this output need next step of program. can't make result a
variable using further. gives me syntaxerror: can't use starred expression here
if try:
a = (*a)
i tried making using zip
, gives me incorrect output, similar described in question zip function giving incorrect output.
<zip object @ 0x0000000001c86108>
so there way unpack list of list , output like:
[1st list of variables], [2nd list of variables], [etc...]
if use itertools get:
l = list(chain(*a)) out: ['[', '4', '1', '6', '2', '1', '3', '6'...
that not required
so working option https://stackoverflow.com/a/46146432/8589220:
row_strings = a.split(",| ") grid = [[int(s) s in row[1:-1].split(", ")] row in row_strings] print(",".join(map(str, grid)))
here's quick , dirty way parse string two-dimensional grid (i.e.: list, contains list of integers):
row_strings = a.split(",| ") grid = [[int(s) s in row[1:-1].split(", ")] row in row_strings] print("\n".join(map(str, grid))) # out: # [416213688, 422393399, 190690902, 81688] # [94925847, 61605626, 346027022] # [1035022, 1036527, 1038016]
Comments
Post a Comment