【问题标题】:Python - Fastest and most efficent way to convert a list-like string to a list of listsPython - 将类似列表的字符串转换为列表列表的最快和最有效的方法
【发布时间】:2017-04-14 22:07:03
【问题描述】:

我正在编写一个 python 程序,它必须运行一个 c++ 程序(已编译)才能管理非常重的负载操作。

我调用这个可执行文件的方式是 subprocess.check_output()

这样做,它会返回一个很长的字符串,如下所示:

executable_output_1 = [0.011, 0.544, 2.314], [7.895, 6.477, 2.573]

executable_output_2 = [4.255, 6.235, 7.566], [9.522, 7.321, 1.234]

type(executable_output) >>> <type 'str'>

在这个例子中我写了一个很短的字符串,但在实际输出中它真的很长。

我想在 python 中对这些数据进行一些操作,所以我需要一个列表列表(因为我会多次调用该可执行文件)。

如何将该字符串转换为列表列表?

期望的输出:

executables_outputs_list = [[[0.011, 0.544, 2.314], [7.895, 6.477, 2.573]], [[4.255, 6.235, 7.566], [9.522, 7.321, 1.234]]]

type(executable_output_list) >>> <type 'list'>

type(executable_output_list[0]) >>> <type 'list'>

type(executable_output_list[0][0][0]) >>> <type 'float'>

【问题讨论】:

  • 我想查看带有 subprocess.check_output() 调用的代码。
  • @Back2Basics 很简单:check_output(['/home/user/executable', '/home/user/image.png'])
  • 好吧,您可能会在= 上拆分并将字符串包装在[] 中,然后使用ast.literal_eval。丑陋,而且肯定是 hackey。
  • @Back2Basics executable_output_1 = check_output(['/home/user/executable', '/home/user/image.png']) 不仅仅是图片,还有一些从中处理的数据。

标签: python list type-conversion


【解决方案1】:

使用ast.literal_eval:

>>> from ast import literal_eval
>>> executable_output = '[0.011, 0.544, 2.314], [7.895, 6.477, 2.573]'
>>> literal_eval(executable_output)
([0.011, 0.544, 2.314], [7.895, 6.477, 2.573])

这是tuple。转换为list

>>> list(literal_eval(executable_output))
[[0.011, 0.544, 2.314], [7.895, 6.477, 2.573]]

【讨论】:

    【解决方案2】:

    您可以使用 python 的json 包,它应该比ast.literal_eval 更快。

    # setup
    from ast import literal_eval
    import json
    
    executable_output_1 = "[0.011, 0.544, 2.314], [7.895, 6.477, 2.573]"
    executable_output_2 = "[4.255, 6.235, 7.566], [9.522, 7.321, 1.234]"
    
    outputs = (executable_output_1, executable_output_2)
    

    json 方法的时序如下:100000 loops, best of 3: 14.5 µs per loop

    def extract(output_strings):
        template = '{{"values": [{}]}}'
        parse_func = lambda x: json.loads(template.format(x))
        return [parse_func(x)["values"] for x in output_strings]
    
    extract(outputs)
    
    >>> [[[0.011, 0.544, 2.314], [7.895, 6.477, 2.573]],
         [[4.255, 6.235, 7.566], [9.522, 7.321, 1.234]]]
    

    ast 方法对于使用 10000 loops, best of 3: 51.6 µs per loop 的虚拟数据要慢 3 倍:

    def extract_ast(output_strings):
        return [list(literal_eval(x)) for x in output_strings]
    
    extract_ast(outputs)
    
    >>> [[[0.011, 0.544, 2.314], [7.895, 6.477, 2.573]],
         [[4.255, 6.235, 7.566], [9.522, 7.321, 1.234]]]
    

    json 方法随着要解析的数据量的增加而改进。通过以下设置,json 方法产生 1000 loops, best of 3: 291 µs per loopast100 loops, best of 3: 3.95 ms per loop 相比要快 13 倍

    executable_output_1 = ",".join(["[0.011, 0.544, 2.314], [7.895, 6.477, 2.573]"] * 100)
    executable_output_2 = ",".join(["[4.255, 6.235, 7.566], [9.522, 7.321, 1.234]"] * 100)
    
    outputs = (executable_output_1, executable_output_2)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-03
      • 1970-01-01
      • 2018-10-13
      • 1970-01-01
      • 1970-01-01
      • 2015-08-13
      • 1970-01-01
      • 2017-11-15
      相关资源
      最近更新 更多