How to unpack a list of lists?

3.4k views Asked by At

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)))
2

There are 2 answers

0
Adriano On

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):

row_strings = a.split(",| ")
grid = [[int(s) for s in row[1:-1].split(", ")] for row in row_strings]
print("\n".join(map(str, grid)))
# Out:
# [416213688, 422393399, 190690902, 81688]
# [94925847, 61605626, 346027022]
# [1035022, 1036527, 1038016]
0
Jared Jensen On

Let's start with the raw output (I'm just manually going to make the string, but this is equivalent to your read method)

a = '[416213688, 422393399, 190690902, 81688],| [94925847, 61605626, 346027022],| [1035022, 1036527, 1038016]'

This is now just one long string. Let's make it a list of strings

a = a.split(',|')

I did ',|' instead of just '|' because you'll want to get rid of the comma too.

Now a looks like this:

>>> ['[416213688, 422393399, 190690902, 81688]', ' [94925847, 61605626, 346027022]', ' [1035022, 1036527, 1038016]']

It's a list of strings that look like arrays. Let's turn those strings into arrays of integers. One way we can do that is with ast.literal_eval(). What it does is takes a string as the argument and attempts to interpret whatever is in that string as a Python literal. It doesn't like leading whitespace, so we remove that first with .strip().

import ast
a = [ast.literal_eval(arr.strip()) for arr in a]

What this does is it takes each string in a, literal_eval's it, and puts those into a list.

This should return the code you want.

[[416213688, 422393399, 190690902, 81688], [94925847, 61605626, 346027022], [1035022, 1036527, 1038016]]