0

有一个带有 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完全陌生,你能帮我吗?

4

3 回答 3

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]]]
于 2013-09-11T17:33:06.977 回答
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]]]
于 2013-09-11T16:52:02.273 回答
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

于 2013-09-11T17:10:45.727 回答