将字符串列表转换为整数列表
Convert list of strings into list of integers
我需要将包含 2 个项目的字符串(用逗号分隔)转换为整数。
来自:
[['(0,3)', '(1,2)', '(2,2)'], ['(0,3)', '(1,2)', '(2,2)']]
至:
[[(0,3), (1,2), (2,2)], [(0,3), (1,2), (2,2)]]
如果您始终按模式输入。
lst = [['(0,3)', '(1,2)', '(2,2)'], ['(0,3)', '(1,2)', '(2,2)']]
new_lst = list(map(lambda i:[eval(a,{}) for a in i],lst))
# OR
# new_lst = list(map(lambda i:list(map(lambda a:eval(a,{}),i)),lst))
print(new_lst)
输出
[[(0, 3), (1, 2), (2, 2)], [(0, 3), (1, 2), (2, 2)]]
lst = [['(0,3)', '(1,2)', '(2,2)'], ['(0,3)', '(1,2)', '(2,2)']]
new_lst = []
for a in lst:
l = []
for i in a:
i = i[1:-1].split(',')
t = []
for num in i:
t.append(int(num))
l.append(tuple(t))
new_lst.append(l)
print(new_lst)
产出
[[(0, 3), (1, 2), (2, 2)], [(0, 3), (1, 2), (2, 2)]]
或使用映射函数
lst = [['(0,3)', '(1,2)', '(2,2)'], ['(0,3)', '(1,2)', '(2,2)']]
new_lst = [list(map(lambda e:tuple(map(lambda a:int(a),e[1:-1].split(','))),i)) for i in lst]
print(new_lst)
[[(0, 3), (1, 2), (2, 2)], [(0, 3), (1, 2), (2, 2)]]
您可以使用 ast.literal_eval
来完成任务:
from ast import literal_eval
lst = [["(0,3)", "(1,2)", "(2,2)"], ["(0,3)", "(1,2)", "(2,2)"]]
lst = [[literal_eval(v) for v in l] for l in lst]
print(lst)
打印:
[[(0, 3), (1, 2), (2, 2)], [(0, 3), (1, 2), (2, 2)]]
编辑:另一种方法(感谢@S3DEV):
out = [list(map(literal_eval, sub_list)) for sub_list in lst]
快速基准测试:
from timeit import timeit
from ast import literal_eval
lst = [["(0,3)", "(1,2)", "(2,2)"], ["(0,3)", "(1,2)", "(2,2)"]]
def fn1():
return [[literal_eval(v) for v in l] for l in lst]
def fn2():
return [list(map(literal_eval, sub_list)) for sub_list in lst]
assert fn1() == fn2()
t1 = timeit(lambda: fn1(), number=1_000)
t2 = timeit(lambda: fn2(), number=1_000)
print(t1)
print(t2)
在我的机器上打印(AMD 3700X,Python 3.9.7):
0.040873110003303736
0.04002662200946361
我需要将包含 2 个项目的字符串(用逗号分隔)转换为整数。
来自:
[['(0,3)', '(1,2)', '(2,2)'], ['(0,3)', '(1,2)', '(2,2)']]
至:
[[(0,3), (1,2), (2,2)], [(0,3), (1,2), (2,2)]]
如果您始终按模式输入。
lst = [['(0,3)', '(1,2)', '(2,2)'], ['(0,3)', '(1,2)', '(2,2)']]
new_lst = list(map(lambda i:[eval(a,{}) for a in i],lst))
# OR
# new_lst = list(map(lambda i:list(map(lambda a:eval(a,{}),i)),lst))
print(new_lst)
输出
[[(0, 3), (1, 2), (2, 2)], [(0, 3), (1, 2), (2, 2)]]
lst = [['(0,3)', '(1,2)', '(2,2)'], ['(0,3)', '(1,2)', '(2,2)']]
new_lst = []
for a in lst:
l = []
for i in a:
i = i[1:-1].split(',')
t = []
for num in i:
t.append(int(num))
l.append(tuple(t))
new_lst.append(l)
print(new_lst)
产出
[[(0, 3), (1, 2), (2, 2)], [(0, 3), (1, 2), (2, 2)]]
或使用映射函数
lst = [['(0,3)', '(1,2)', '(2,2)'], ['(0,3)', '(1,2)', '(2,2)']]
new_lst = [list(map(lambda e:tuple(map(lambda a:int(a),e[1:-1].split(','))),i)) for i in lst]
print(new_lst)
[[(0, 3), (1, 2), (2, 2)], [(0, 3), (1, 2), (2, 2)]]
您可以使用 ast.literal_eval
来完成任务:
from ast import literal_eval
lst = [["(0,3)", "(1,2)", "(2,2)"], ["(0,3)", "(1,2)", "(2,2)"]]
lst = [[literal_eval(v) for v in l] for l in lst]
print(lst)
打印:
[[(0, 3), (1, 2), (2, 2)], [(0, 3), (1, 2), (2, 2)]]
编辑:另一种方法(感谢@S3DEV):
out = [list(map(literal_eval, sub_list)) for sub_list in lst]
快速基准测试:
from timeit import timeit
from ast import literal_eval
lst = [["(0,3)", "(1,2)", "(2,2)"], ["(0,3)", "(1,2)", "(2,2)"]]
def fn1():
return [[literal_eval(v) for v in l] for l in lst]
def fn2():
return [list(map(literal_eval, sub_list)) for sub_list in lst]
assert fn1() == fn2()
t1 = timeit(lambda: fn1(), number=1_000)
t2 = timeit(lambda: fn2(), number=1_000)
print(t1)
print(t2)
在我的机器上打印(AMD 3700X,Python 3.9.7):
0.040873110003303736
0.04002662200946361