如何将字符串列表转换为python中的整数[复制]

时间:2021-04-04 15:58:56

This question already has an answer here:

这个问题已经有了答案:

In my python Script I have:

在我的python脚本中:

user = nuke.getInput("Frames Turned On")
userLst = [user]
print userLst

Result:

结果:

['12,33,223']

I was wondering How I would remove the ' in the list, or somehow convert it into int?

我想知道如何删除列表中的“列表”,或者将它转换成int类型?

6 个解决方案

#1


19  

Use split() to split at the commas, use int() to convert to integer:

使用split()在逗号分隔,使用int()转换为整数:

user_lst = map(int, user.split(","))

#2


9  

There's no ' to remove in the list. When you print a list, since it has no direct string representation, Python shows you its repr—a string that shows its structure. You have a list with one item, the string 12,33,223; that's what [user] does.

在列表中没有“删除”。当您打印一个列表时,由于它没有直接的字符串表示,Python会向您显示它的repra - a字符串,它显示了它的结构。您有一个单项的列表,字符串12,33,223;这是什么[用户]。

You probably want to split the string by commas, like so:

您可能希望用逗号分隔字符串:

user_list = user_input.split(',')

If you want those to be ints, you can use a list comprehension:

如果你想要那些是ints,你可以使用列表理解:

user_list = [int(number) for number in user_input.split(',')]

#3


1  

[int(s) for s in user.split(",")]

I have no idea why you've defined the separate userLst variable, which is a one-element list.

我不知道为什么定义了单独的userLst变量,它是一个单元素列表。

#4


1  

>>> ast.literal_eval('12,33,223')
(12, 33, 223)

#5


-1  

>>> result = ['12,33,223']
>>> int(result[0].replace(",", ""))
1233233
>>> [int(i) for i in result[0].split(',')]
[12, 33, 233]

#6


-1  

You could use the join method and convert that to an integer:

可以使用join方法将其转换为整数:

int(''.join(userLst))    

1233223

1233223

#1


19  

Use split() to split at the commas, use int() to convert to integer:

使用split()在逗号分隔,使用int()转换为整数:

user_lst = map(int, user.split(","))

#2


9  

There's no ' to remove in the list. When you print a list, since it has no direct string representation, Python shows you its repr—a string that shows its structure. You have a list with one item, the string 12,33,223; that's what [user] does.

在列表中没有“删除”。当您打印一个列表时,由于它没有直接的字符串表示,Python会向您显示它的repra - a字符串,它显示了它的结构。您有一个单项的列表,字符串12,33,223;这是什么[用户]。

You probably want to split the string by commas, like so:

您可能希望用逗号分隔字符串:

user_list = user_input.split(',')

If you want those to be ints, you can use a list comprehension:

如果你想要那些是ints,你可以使用列表理解:

user_list = [int(number) for number in user_input.split(',')]

#3


1  

[int(s) for s in user.split(",")]

I have no idea why you've defined the separate userLst variable, which is a one-element list.

我不知道为什么定义了单独的userLst变量,它是一个单元素列表。

#4


1  

>>> ast.literal_eval('12,33,223')
(12, 33, 223)

#5


-1  

>>> result = ['12,33,223']
>>> int(result[0].replace(",", ""))
1233233
>>> [int(i) for i in result[0].split(',')]
[12, 33, 233]

#6


-1  

You could use the join method and convert that to an integer:

可以使用join方法将其转换为整数:

int(''.join(userLst))    

1233223

1233223