简体   繁体   中英

Convert strings of list to list of integers inside a list

I have a list of strings and those strings are lists. Like this: ['[1,2,3]','[10,12,5]'] , for example. I want to get a list of lists or even every list there:

[[1,2,3],[10,12,5]]

You should use literal_eval to get actual list object from string .

>>> from ast import literal_eval
>>> data =  ['[1,2,3]','[10,12,5]']
>>> data = [literal_eval(each) for each in data]
>>> data
[[1, 2, 3], [10, 12, 5]]

literal_eval safely evaluates each object.

>>> help(literal_eval)
Help on function literal_eval in module ast:

literal_eval(node_or_string)
    Safely evaluate an expression node or a string containing a Python
    expression.  The string or node provided may only consist of the following
    Python literal structures: strings, numbers, tuples, lists, dicts, booleans,
    and None.

You should always try to avoid using eval function. Read more here .

You can use ast.literal_eval , eval should generally be avoided:

l= ['[1,2,3]','[10,12,5]']

from ast import literal_eval

print([literal_eval(ele) for ele in l])
[[1, 2, 3], [10, 12, 5]]

Or index, split and map:

print([list(map(int,ele[1:-1].split(","))) for ele in l])
[[1, 2, 3], [10, 12, 5]]

If you always have the same format splitting is the most efficient solution:

In [44]: %%timeit                       
l= ['[1,2,3]','[10,12,5]']
l = [choice(l) for _ in range(1000)]
[eval(ele) for ele in l]
   ....: 
100 loops, best of 3: 8.15 ms per loop

In [45]: %%timeit
l= ['[1,2,3]','[10,12,5]']
l = [choice(l) for _ in range(1000)]
[literal_eval(ele) for ele in l]
   ....: 
100 loops, best of 3: 11.4 ms per loop

In [46]: %%timeit                       
l= ['[1,2,3]','[10,12,5]']
l = [choice(l) for _ in range(1000)]
[list(map(int,ele[1:-1].split(","))) for ele in l]
   ....: 
100 loops, best of 3: 2.07 ms per loop

You use ast.literal_eval

import ast
l =  ['[1,2,3]','[10,12,5]']
l = [ast.literal_eval(i) for i in l]

It is often not recommended, but you could use eval for each string:

fun = ['[1,2,3]','[10,12,5]']
listy = [eval(x) for x in fun]

Outputs:

[[1, 2, 3], [10, 12, 5]]

This question provides excellent reason as to why using eval can be a bad idea. I am just presenting it here as an option. eval can pose a large security risk, as you are giving user input the full power of the Python interpreter. Additionally, it violates one of the fundamental principles of programming, which states that all of your executable code should directly map to your source code. Evaluating user input with eval leads to executable code that evidently isn't in your source.

Using ast.literal_eval() is preferable, and Padraic Cunningham's answer addresses how to use it effectively.

使用正则表达式:

new_list = [[int(j) for j in re.findall(r'\d+', i)] for i in old_list]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM