简体   繁体   English

将列表字符串转换为列表中的整数列表

[英]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. 像这样: ['[1,2,3]','[10,12,5]'] ,例如。 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 . 您应该使用literal_evalstring获取实际的list对象。

>>> 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. literal_eval安全地评估每个对象。

>>> 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. 您应该始终避免使用eval函数。 Read more here . 在这里阅读更多。

You can use ast.literal_eval , eval should generally be avoided: 您可以使用ast.literal_eval ,通常应避免使用eval

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 您使用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: 通常不建议这样做,但是您可以对每个字符串使用eval

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. 这个问题为为什么使用eval可能是个坏主意提供了很好的理由。 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. eval可能会带来很大的安全风险,因为您正在为用户提供Python解释器的全部功能。 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. 使用eval评估用户输入会导致显然不在您的源代码中的可执行代码。

Using ast.literal_eval() is preferable, and Padraic Cunningham's answer addresses how to use it effectively. 最好使用ast.literal_eval()Padraic Cunningham的答案针对如何有效使用它。

使用正则表达式:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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