矩阵转换为python中的列表

时间:2013-09-11 16:44:40

标签: python list matrix

有一个带有Wolfram Mathematica矩阵式的txt文件(可以是1000多个字符串):

{-0.00036, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0.0003, -1.00026, 2, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0.0002, -2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}

我需要从中创建嵌套列表以在python中进行进一步的计算。 结果应该是 -

s=[[[0.00036], [1], [0], [1], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0]],[[0.0003],.. and so on

我是python的新手,你能帮助我吗?

3 个答案:

答案 0 :(得分:2)

eval是邪恶的,但有时它有它的位置。由于输入数据包含算术表达式,eval是转换它的最简单方法。

当然,不要在不受信任的输入上运行此代码,因为它可能会危害您的计算机。 (如果这是你用Mathematica生成的所有东西,你应该非常安全。)

以下是代码:

m = eval('[' + text.replace('{','[').replace('}',']') + ']')
m = [[[x] for x in row] for row in m]

111.txt文件上运行此操作(使用text = open('111.txt').read())会产生预期结果:

[[[-0.00036], [1], [0], [1], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0]], [[0.0003], [-1.00026], [2], [0], [1], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0]], [[0], [0.0002], [-2], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0]], [[1e-05], [0], [0], [-1], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0]], [[0], [1e-05], [0], [0], [-1], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0]], [[5e-05], [0], [0], [0], [0], [-0.01], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0]], [[0], [5e-05], [0], [0], [0], [0], [-0.01], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0]], [[0], [0], [0], [0], [0], [0.01], [0], [-0.000344], [1], [0], [1], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0]], [[0], [0], [0], [0], [0], [0], [0.01], [0.0003], [-1.000244], [2], [0], [1], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0]], [[0], [0], [0], [0], [0], [0], [0], [0], [0.0002], [-2], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0]], [[0], [0], [0], [0], [0], [0], [0], [1e-05], [0], [0], [-1], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0]], [[0], [0], [0], [0], [0], [0], [0], [0], [1e-05], [0], [0], [-1], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0]], [[0], [0], [0], [0], [0], [0], [0], [3.4e-05], [0], [0], [0], [0], [-0.01], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0]], [[0], [0], [0], [0], [0], [0], [0], [0], [3.4e-05], [0], [0], [0], [0], [-0.01], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0]], [[0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0.01], [0], [-0.000328], [1], [0], [1], [0], [0], [0], [0], [0], [0]], [[0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0.01], [0.0003], [-1.000228], [2], [0], [1], [0], [0], [0], [0], [0]], [[0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0.0002], [-2], [0], [0], [0], [0], [0], [0], [0]], [[0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [1e-05], [0], [0], [-1], [0], [0], [0], [0], [0], [0]], [[0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [1e-05], [0], [0], [-1], [0], [0], [0], [0], [0]], [[0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [1.8e-05], [0], [0], [0], [0], [-0.01], [0], [0], [0], [0]], [[0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [1.8e-05], [0], [0], [0], [0], [-0.01], [0], [0], [0]], [[0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0.01], [0], [-0.0003], [1], [0]], [[0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0.01], [0.0003], [-1.0002], [2]], [[0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0.0002], [-2]]]

答案 1 :(得分:1)

from ast import literal_eval
with open('filename') as f:
    lis = [[[abs(literal_eval(item))] for item in 
                     line.rstrip('},\n').lstrip('{').split(', ')] for line in f]
    print lis
...     
[[[0.00036], [1], [0], [1], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0]], [[0.0003], [1.00026], [2], [0], [1], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0]], [[0], [0.0002], [2], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0]]]

答案 2 :(得分:1)

以下是一些允许换行符在您喜欢的地方的代码。但条目只能是浮点数。

f = open("filename", "r")

buff = ""
array = []
for line in f:
    buff += line
    if "}" in line:
        start = buff.index("{")
        end = buff.index("}")
        array.append([float(val.strip()) for val in buff[start+1:end].split(",")])
        buff = buff[end+1:]

f.close()

print(array)

如果你需要算术表达式到一个乘法级别的扩展,你可以这样做:

import operator

def prod(lst):
    return reduce(operator.mul, lst, 1)

f = open("filename", "r")

buff = ""
array = []
for line in f:
    buff += line
    if "}" in line:
        start = buff.index("{")
        end = buff.index("}")
        array.append([prod(float(factor.strip()) for factor in val.split("*")) for val in buff[start+1:end].split(",")])
        buff = buff[end+1:]

f.close()

print(array)

要了解正在发生的事情,请查看List Comprehensionsstr.split作为开始。