What is the fastest way to convert string to array in python?

后端 未结 4 1588
北荒
北荒 2020-12-07 02:11

This is a line I read from a text file:

[54, 95, 45, -97, -51, 84, 0, 32, -55, 14, 50, 54, 68, -3, 57, 88, -1]

I used readline() to read it

4条回答
  •  广开言路
    2020-12-07 02:59

    I'm not sure that this is the fastest, but it's definitely the safest/easiest:

    import ast
    lst = ast.literal_eval(s)
    

    regular eval would work too:

    lst = eval(s)
    

    Some basic timings from my machine:

    >>> s = '[54, 95, 45, -97, -51, 84, 0, 32, -55, 14, 50, 54, 68, -3, 57, 88, -1]' 
    >>> def f1():
    ...    eval(s)
    ... 
    >>> def f2():
    ...    ast.literal_eval(s)
    ... 
    >>> timeit.timeit('f1()', 'from __main__ import f1')
    31.415852785110474
    >>> timeit.timeit('f2()', 'from __main__ import f2')
    46.25958704948425
    

    So, according to my computer, eval is about 50% faster than ast.literal_eval. However, eval is terribly unsafe and should never be used on any string unless you trust it completely. Unless this is a real demonstratable bottleneck and you trust the input 100%, I would consider the little bit of extra time worth it in exchange for being able to sleep soundly at night.

提交回复
热议问题