将字符串列表转换为正确的类型
Convert list of strings into correct type
我正在尝试将标记化的字符串列表转换为适当类型的列表。调用此函数 typeChange 以 return 字符串的适当类型版本。
到目前为止,我的代码执行以下所有操作,但最后一种情况除外。有解决这个问题的简单方法吗?
例如:
"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 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 = list(map(int, (c.strip('][').split(' '))))
return (len(retList), retList)
if c=='True':
return True
elif c=='False':
return False
return int(c)
except:
return c
我的预期结果适用于所有提供的案例,除非 string/variable 在列表中:
预期:'[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]'
与输入字符串相同,这意味着 except
块中的行 return c
被触发,所以 try
块引发错误。
错误出现在行 retList = list(map(int, (c.strip('][').split(' '))))
中,这是由于对并非全是数字字符串的值列表执行 map(int, ...)
造成的;其中之一是字符串 'x'
。 int('x')
抛出 ValueError
,因此观察到的行为。
要解决此问题,请将 map(int, ...)
替换为 map(typeChange, ...)
,以递归方式将数字字符串映射到整数,同时将非数字字符串(如 '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()
到 return 布尔值:
'x'.isdigit()
False
'1'.isdigit()
True
我正在尝试将标记化的字符串列表转换为适当类型的列表。调用此函数 typeChange 以 return 字符串的适当类型版本。
到目前为止,我的代码执行以下所有操作,但最后一种情况除外。有解决这个问题的简单方法吗?
例如:
"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 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 = list(map(int, (c.strip('][').split(' '))))
return (len(retList), retList)
if c=='True':
return True
elif c=='False':
return False
return int(c)
except:
return c
我的预期结果适用于所有提供的案例,除非 string/variable 在列表中:
预期:'[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]'
与输入字符串相同,这意味着 except
块中的行 return c
被触发,所以 try
块引发错误。
错误出现在行 retList = list(map(int, (c.strip('][').split(' '))))
中,这是由于对并非全是数字字符串的值列表执行 map(int, ...)
造成的;其中之一是字符串 'x'
。 int('x')
抛出 ValueError
,因此观察到的行为。
要解决此问题,请将 map(int, ...)
替换为 map(typeChange, ...)
,以递归方式将数字字符串映射到整数,同时将非数字字符串(如 '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()
到 return 布尔值:
'x'.isdigit()
False
'1'.isdigit()
True