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

My original suggestion with a list comprehension.

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.

Map is usually faster than comprehension when using a builtin functions, and slower if you are using a user defined function (including lambda) – gnibbler Oct 15 '09 at 20:30.

(xval,yval,zval) = int(s) for s in file. Split('-').

I'd declare this answer to be efficient as in "easiest to code and move on to other more important stuff, and to understand when I come back to it later". Trying to eke out microseconds of performance from this particular line of code is clearly a detour into the Tarpit of Unnecessary Optimization. – Paul McGuire Oct 15 '09 at 21:56 I generally agree and that is exactly why I did not brush it up (although I am aware of the generator expressions and the like).

But, since the matter in question is about three elements it might as well be left as is – Bartosz Radaczy? Ski Oct 16 '09 at 20:09.

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).

Ski Oct 15 '09 at 20:11 2 Don't worry, if you are assigning the map() to a tuple it will still work in Python 3 – gnibbler Oct 15 '09 at 20:28.

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('-')).

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions