int函数使用方法python,在Python中将字符串从split函数转换为int的有效方法

I have a string of data with the following format: xpos-ypos-zoom (i.e. 8743-12083-15) that I want to split up and store in the variables xpos, ypos, and zoom. Since I need to do some calculations with these number I'd like to convert them to integers right from the beginning. Currently, the way I'm doing this is with the following code:

file = '8743-12083-15'

xval, yval, zoom = file.split("-")

xval = int(xval)

yval = int(yval)

It seems to me there should be a more efficient way of doing this. Any ideas?

解决方案

My original suggestion with a list comprehension.

test = '8743-12083-15'

lst_int = [int(x) for x in test.split("-")]

EDIT:

As to which is most efficient (cpu-cyclewise) is something that should always be tested.

Some quick testing o

你可能感兴趣的:(int函数使用方法python)