3

I have the list of strings:

['[12 9 15]','[98 12 18]','[56 45 45]'] 

and I want to convert it to

[[12,9,15],[98,12,18],[56,45,45]] 
1
  • 3
    How did you end up with ['[12 9 15]','[98 12 18]','[56 45 45]'] ? Commented Jun 24, 2016 at 21:43

6 Answers 6

6

You can use split inside a list comprehension to do this.

As [1 2 3] is not the proper representation of a python list in a string, we can remove the brackets to get '1 2 3' which on splitting becomes ['1', '2', '3']. This can be easily converted to a integer nested list by casting it to an int using the int callable.

>>> l = ['[12 9 15]','[98 12 18]','[56 45 45]'] >>> [[int(j) for j in i[1:-1].split()] for i in l] [[12, 9, 15], [98, 12, 18], [56, 45, 45]] 

For further reading What does "list comprehension" mean? How does it work and how can I use it?

Sign up to request clarification or add additional context in comments.

6 Comments

@henshalom Yes, Use the float buitin. [[float(j) for j in i[1:-1].split()] for i in l]
It works, but when I run it on my list i get [12, enter 15, enter 455 enter ... it skips lines
@henshalom Does your string input contain newlines? If yes, then strip them out. If you can provide a example input with the newlines, I can certainly help you out.
@henshalom Oops, You are using numpy to read a csv file? You can use a csv.reader that handles your style of data much better. Is it possible to create a minimal rep of that data? 180 lines is too much for a minimal reproducible example.
|
2

Your strings [12 9 15] aren't formatted like python lists (commas are missing). You've got a couple options depending on how robust your parser needs to be:

import ast out_list = [] for string_list in list_of_strings: list_repr = ','.join(string_list.split()) out_list.append(ast.literal_eval(list_repr)) 

This will work so long as you don't have any inner strings formatted like:

'[ 12 9, 5] (the leading space will mess it up)

I think that probably the most robust parser that I can think of is to remove the [ and ] and them parse it yourself:

out_list = [] for string_list in list_of_strings: str_items = string_list.replace('[', '').replace(']', '') out_list.append([int(item) for item in str_items.split()]) 

2 Comments

Why aren't you going by the replace way? string_list.replace(' ',','). Apologies, If I am missing something there.
@BhargavRao -- If there are more than one space in a row in the string_list, that'll fail. We want to replace consecutive runs of spaces with a single comma and str.replace doesn't do that well. It's a pretty easy re.sub if we wanted to go that route, but I thought I'd save OP from having to learn some regular expressions for this simple task.
2

As long as the strings are fairly regular, this should work:

>>> x = ['[12 9 15]','[98 12 18]','[56 45 45]'] >>> x = [[int(i) for i in string.strip('[]').split()] for string in x] >>> x [[12, 9, 15], [98, 12, 18], [56, 45, 45]] 

Comments

1

Use a regular expression

[map(int, re.findall('\d+', item)) for item in x] 

In case it is not always well-formated.


>>> import re >>> [map(int, re.findall('\d+', item)) for item in x] [[12, 9, 15], [98, 12, 18], [56, 45, 45]] 

1 Comment

And list(map(...)) on python3.x ... but I guess that's irrelevant since it's tagged python2.7.
0

The simpler the solution, the better it is for others to understand.

Well here is my solution:

list_of_strings = ['[12 9 15]','[98 12 18]','[56 45 45]'] list_of_lists = [map(int, x[1:-1].split()) for x in list_of_strings] 

So I using list-comprehension here. The 'map' function returns a list. The code x[1:-1].split() will split each string on space character(s) and the each string token would then be converted to 'int' which is the function I've passed to the map function.

Need more explanation over my code?

Comments

0

Please check if this is helpful.

>>> x = ['[12 9 15]','[98 12 18]','[56 45 45]'] >>> print eval(str([ item.replace(" ",",") for item in x ]).replace("'", '')) [[12, 9, 15], [98, 12, 18], [56, 45, 45]] 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.