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

后端 未结 4 1578
北荒
北荒 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 03:04

    If that's the string, go here http://docs.python.org/2/library/functions.html#eval

    >>> s = "[54, 95, 45, -97, -51, 84, 0, 32, -55, 14, 50, 54, 68, -3, 57, 88, -1]"
    >>> eval(s)
    [54, 95, 45, -97, -51, 84, 0, 32, -55, 14, 50, 54, 68, -3, 57, 88, -1]
    

提交回复
热议问题