Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/347.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
Python 将字符串列表转换为正确的类型_Python_Postscript - Fatal编程技术网

Python 将字符串列表转换为正确的类型

Python 将字符串列表转换为正确的类型,python,postscript,Python,Postscript,我正在尝试将一个标记化的字符串列表转换为一个适当类型的列表。调用此函数typeChange以返回字符串的适当类型版本 到目前为止,我的代码除了最后一种情况外,还执行以下所有操作。有没有简单的方法来解决这个问题 例如: "1" -> 1 "True" -> True "\str" -> "\str" "[1 2 3 4 5]" -> [1,2,3,4,5] "[1 2 3 4 5 x]" -> [1,2,3,4,5,"x"] 这是我的简化PostScript解释器的

我正在尝试将一个标记化的字符串列表转换为一个适当类型的列表。调用此函数typeChange以返回字符串的适当类型版本

到目前为止,我的代码除了最后一种情况外,还执行以下所有操作。有没有简单的方法来解决这个问题

例如:

"1" -> 1
"True" -> True
"\str" -> "\str"
"[1 2 3 4 5]" -> [1,2,3,4,5]
"[1 2 3 4 5 x]" -> [1,2,3,4,5,"x"]
这是我的简化PostScript解释器的typeChange函数

def类型更改(c):
尝试:
如果c中的“[”:#检查它是否是一个列表,出于某种原因不能执行或“]”,因为int作为列表返回
retList=list(map(int,(c.strip('][')).split(''))
返回(len(retList),retList)
如果c=='True':
返回真值
elif c=='False':
返回错误
返回int(c)
除:
返回c
我的预期结果适用于所有提供的情况,但字符串/变量位于列表中时除外:

应为:
'[1 x 2 3 4]
->
[1,“x”,2,3,4]


实际值:
'[1 x 2 3 4]'
->
'[1 x 2 3 4]'

由于输出字符串
'[1 x 2 3 4]'
与输入字符串相同,这意味着
块中的
行返回c
被触发,因此
try
块中的某些内容引发错误

def typeChange(c):
    try:
        if '[' in c:    # checking if it is a list, for some reason can't do or ']', because ints return as a list
            retList = [int(x) if x.isdigit() else str(x) for x in c.strip('[]').split(' ')]
            return retList
        if c=='True':
            return True
        elif c=='False':
            return False
        return int(c)
    except:
        return c

typeChange('[1 2 3 4 5 x]')
[1, 2, 3, 4, 5, 'x']
错误出现在
retList=list(map(int,(c.strip('][')).split(''))
行中,这是由于对并非所有数字字符串的值列表执行
map(int,…)
而引起的;其中一个是字符串
'x'
int('x')
抛出一个
ValueError
,因此观察到的行为

要解决此问题,请将
map(int,…)
替换为
map(typeChange,…)
,以递归方式将数字字符串映射到int,同时将非数字字符串(如
'x'
)保留为字符串,而不会引发错误

def typeChange(c):
    try:
        if '[' in c:    # checking if it is a list, for some reason can't do or ']', because ints return as a list
            retList = [int(x) if x.isdigit() else str(x) for x in c.strip('[]').split(' ')]
            return retList
        if c=='True':
            return True
        elif c=='False':
            return False
        return int(c)
    except:
        return c

typeChange('[1 2 3 4 5 x]')
[1, 2, 3, 4, 5, 'x']
让我们来看看类型:

type(typeChange('[1 2 3 4 5 x]'))
list
如果字符串表示数字,我们使用
str.isdigit()
返回布尔值:

'x'.isdigit()
False
'1'.isdigit()
True

如果列表中的所有内容不一定都是int,那么使用
map(typeChange,…)
而不是
map(int,…)
怎么样?如果您使用python语法来编写列表(
“[1,2,3,4,\'a\”]”而不是
[1,2,3,4 a]
)您可以使用。我能够推断错误来自何处,但不知道如何修复。我甚至不知道这在Python中会起作用。这很有意义!您太棒了,谢谢!没问题。为了澄清,
c.strip('][').split(''))
生成一个字符串列表,
map
函数通过对列表中的每个元素应用一些函数(在本例中为
int
typeChange
)来转换列表。您可以将喜欢的任何函数传递到
map
,只要它对单个列表元素起作用。