Skip to content Skip to sidebar Skip to footer

Fast Method Of Converting All Strings In A List To Integers

I've gone through the Convert all strings in a list to int post I want to convert results = ['1', '2', '3'] to: results = [1, 2, 3] I know i can do it by map(int, results)

Solution 1:

Multiprocessing is a way to get it done faster (in addition of using Numpy):

E.g:

In [11]: from multiprocessing import Pool

In [12]: pool = Pool(10)

In [13]: pool.map(int, [str(i) for i inrange(500)])

Numpy will mostly provide a memory gain as you would be dealing with primitive types instead of python objects, but will also provide a non-negligible speed gain as well. Optimizing time of an iteration like this is always done by using parallelization so I advise using both Numpy and a process pool.

Post a Comment for "Fast Method Of Converting All Strings In A List To Integers"