Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将字符串转换为int-python_Python - Fatal编程技术网

将字符串转换为int-python

将字符串转换为int-python,python,Python,如何在python中将字符串转换为int 假设我有这个数组 ['(111,11,12)','(12,34,56)'] to [(111,11,12),(12,34,56)] 任何帮助都将不胜感激,谢谢 import ast a = "['(111,11,12)','(12,34,56)']" [ast.literal_eval(b) for b in ast.literal_eval(a)] # [(111, 11, 12), (12, 34, 56)] 编辑:如果您有一个字符串列表(而不是

如何在python中将字符串转换为int 假设我有这个数组

['(111,11,12)','(12,34,56)'] to [(111,11,12),(12,34,56)]
任何帮助都将不胜感激,谢谢

import ast
a = "['(111,11,12)','(12,34,56)']"
[ast.literal_eval(b) for b in ast.literal_eval(a)]
# [(111, 11, 12), (12, 34, 56)]
编辑:如果您有一个字符串列表(而不是字符串),就像@DSM建议的那样,那么您必须修改它:

a = ['(111,11,12)','(12,34,56)']
[ast.literal_eval(b) for b in a]
# [(111, 11, 12), (12, 34, 56)]
编辑:如果您有一个字符串列表(而不是字符串),就像@DSM建议的那样,那么您必须修改它:

a = ['(111,11,12)','(12,34,56)']
[ast.literal_eval(b) for b in a]
# [(111, 11, 12), (12, 34, 56)]

可以使用int()关键字将字符串转换为int:

但您给出的示例似乎表明您希望对整个元组而不是单个整数执行此操作。如果是,您可以使用内置的评估功能:

>>> eval('(111,111)')
(111, 111)

可以使用int()关键字将字符串转换为int:

但您给出的示例似乎表明您希望对整个元组而不是单个整数执行此操作。如果是,您可以使用内置的评估功能:

>>> eval('(111,111)')
(111, 111)
您可以尝试一些re:

import re
src = ['(111,11,12)', '(12,34,56)']
[tuple([int(n) for n in re.findall(r"(\d+),(\d+),(\d+)", s)[0]]) for s in src]
您可以尝试一些re:

import re
src = ['(111,11,12)', '(12,34,56)']
[tuple([int(n) for n in re.findall(r"(\d+),(\d+),(\d+)", s)[0]]) for s in src]

通过阅读您的问题,我看到您有一个字符串列表:

l = ['(111,11,12)','(12,34,56)']
你想把它转换成一个数字列表

# some cleaning first
number_list = [x.strip('()').split(',') for x in l]
for numbers in number_list:
    numbers[:] = [int(x) for x in numbers]
print number_list

对于列表理解解析,很抱歉,如果您是新手,它看起来很奇怪,但却是一个非常常见的python习语,您应该熟悉它。

通过阅读您的问题,我看到您有一个字符串列表:

l = ['(111,11,12)','(12,34,56)']
你想把它转换成一个数字列表

# some cleaning first
number_list = [x.strip('()').split(',') for x in l]
for numbers in number_list:
    numbers[:] = [int(x) for x in numbers]
print number_list
很抱歉,如果你是新手,这看起来很奇怪,但却是一个非常常见的python习惯用法,你应该熟悉它。

玩得开心

def customIntparser(n):
    exec("n="+str(n))
    if type(n) is list or type(n) is tuple:
        temps=[]
        for a in n:
            temps.append(customIntparser(str(a)))
        if type(n) is tuple:
            temps=tuple(temps)
        return temps
    else:
        exec("z="+str(n))
        return z
样本测试:

>>>s = "['(111,11,12)','(12,34,56)']"
>>>a=customIntparser(s)
>>> a
# [(111, 11, 12), (12, 34, 56)]
>a[0][1]
# 11
玩得开心

def customIntparser(n):
    exec("n="+str(n))
    if type(n) is list or type(n) is tuple:
        temps=[]
        for a in n:
            temps.append(customIntparser(str(a)))
        if type(n) is tuple:
            temps=tuple(temps)
        return temps
    else:
        exec("z="+str(n))
        return z
样本测试:

>>>s = "['(111,11,12)','(12,34,56)']"
>>>a=customIntparser(s)
>>> a
# [(111, 11, 12), (12, 34, 56)]
>a[0][1]
# 11

@closevoter:我看不出有任何理由认为这应该过于本地化,尽管您可能会选择将其作为一个整体进行投票duplicate@closevoter:我看不出有任何理由认为这应该过于本地化,尽管你可以选择将其作为副本进行投票-1:你的答案对OP的问题最没有帮助,请不要建议可怕的评估。-1:你的答案是对OP的问题帮助最小,请不要建议可怕的评估。OP似乎有一个字符串列表,而不是一个字符串(当然,修改很简单。)@Goranek
ast。literal_eval
与内置的
eval
不同-即使在处理来自不可信来源的数据时,使用它也是完全安全的。有关更多信息,请参阅。OP似乎有一个字符串列表,而不是一个字符串(当然,修改很简单。)@Goranek
ast。literal_eval
不同于内置的
eval
——即使在处理来自不受信任来源的数据时,使用它也是完全安全的。有关更多信息,请参阅。