Efficient way to convert strings from split function to ints in Python

后端 未结 4 1359
执笔经年
执笔经年 2020-12-02 20:08

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

相关标签:
4条回答
  • 2020-12-02 20:32

    efficient as in fewer lines of code?

    (xval,yval,zval) = [int(s) for s in file.split('-')]
    
    0 讨论(0)
  • 2020-12-02 20:33

    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 on my Python 2.6 install indicates map is probably the most efficient candidate here (building a list of integers from a value-splitted string). Note that the difference is so small that this does not really matter until you are doing this millions of times (and it is a proven bottleneck)...

    def v1():
     return [int(x) for x in '8743-12083-15'.split('-')]
    
    def v2():
     return map(int, '8743-12083-15'.split('-'))
    
    import timeit
    print "v1", timeit.Timer('v1()', 'from __main__ import v1').timeit(500000)
    print "v2", timeit.Timer('v2()', 'from __main__ import v2').timeit(500000)
    
    > output v1 3.73336911201 
    > output v2 3.44717001915
    
    0 讨论(0)
  • 2020-12-02 20:39

    You can map the function int on each substring, or use a list comprehension:

    >>> file = '8743-12083-15'
    >>> list(map(int, file.split('-')))
    [8743, 12083, 15]
    >>> [int(d) for d in file.split('-')]
    [8743, 12083, 15]
    

    In the above the call to list is not required, unless working with Python 3.x. (In Python 2.x map returns a list, in Python 3.x it returns a generator.)

    Directly assigning to the three variables is also possible (in this case a generator expression instead of a list comprehension will do):

    >>> xval, yval, zval = (int(d) for d in file.split('-'))
    >>> xval, yval, zval
    (8743, 12083, 15)
    
    0 讨论(0)
  • 2020-12-02 20:50

    note: you might want to pick a different name for file as it shadows the buildtin

    this works in Python 2 and 3

    
    xval,yval,zval = map(int,file.split('-'))
    
    0 讨论(0)
提交回复
热议问题