将不规则形状的数组导入 Python

我有一些在 Mathematica 中生成的数据,我需要将它们导入 Python。数据的生成方式依赖于符号计算,因此简单地在 Python 中生成它是不可能的。数据是一个维度数组 (126,2) 但是,其中每个元素中的第一个位置是一个整数,第二个位置是一个列表列表,并且元素之间的维度不是恒定的,例如:


`

    {

    {-9,{{4,2},{5,6},{8,10}}},

    {-2,{{3,6},{6,1}}}

    {4,{{3,6},{6,1},{3,6},{6,1},{3,6},{6,1},{3,6},{6,1}}}

    }

`

将是前三个元素。每个元素中的第二个位置总是一个二维列表。这里的目标是将这些数据作为一个 numpy 数组导入,这样我就可以调用每个元素,无论它的位置如何。


我取得了一些成功,numpy.genfromtxt("data.txt",delimiters="}}}")它为我提供了正确的形状 (126,2),但每个元素都只是“nan”。


我取得了更大的成功


`

with open("data.csv") as csvfile:

     reader = csv.reader(csvfile, delimiter=' ')

     for element in reader:

         print(np.asarray(element)[0])

`

这给了我作为数组的整数值,这很棒!对于我尝试过的每个元素中的第二个位置:


`

def replace_all(text, dic):

    for i, j in dic.items():

        text = text.replace(i, j)

    return text

d={"{":"[","}":"]"}

with open("spinweights.csv") as csvfile:

     reader = csv.reader(csvfile, delimiter=',')

     it=0

     for element in reader:

         while it<1:

             curlToSq=replace_all(str(element[1]),d)

             print(np.asarray(curlToSq))

`

该replace_all函数正在更改方括号中的所有大括号(这里的想法是这样可以更容易地转换为 numpy 数组)。那里的最后一行确实返回了一个数组...形状(),其中没有一个对象可下标,这正是我所需要的!


任何帮助表示赞赏。


函数式编程
浏览 222回答 2
2回答

FFIVE

我认为将其转换为列表结构将是最简单的。我,在以下内容中添加了一个:In [22]: astr=""" {&nbsp;&nbsp; &nbsp; ...:&nbsp; &nbsp; &nbsp;{-9,{{4,2},{5,6},{8,10}}},&nbsp;&nbsp; &nbsp; ...:&nbsp; &nbsp; &nbsp;{-2,{{3,6},{6,1}}},&nbsp;&nbsp; &nbsp; ...:&nbsp; &nbsp; &nbsp;{4,{{3,6},{6,1},{3,6},{6,1},{3,6},{6,1},{3,6},{6,1}}}&nbsp;&nbsp; &nbsp; ...:&nbsp; &nbsp; &nbsp;}"""&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;In [23]: astr1=astr.replace('{','[').replace('}',']').replace('\n','')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;In [24]: astr1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;Out[24]: ' [&nbsp; &nbsp; [-9,[[4,2],[5,6],[8,10]]],&nbsp; &nbsp; [-2,[[3,6],[6,1]]],&nbsp; &nbsp; [4,[[3,6],[6,1],[3,6],[6,1],[3,6],[6,1],[3,6],[6,1]]]&nbsp; &nbsp; ]'是各种字符串评估器。 eval始终可用。 ast更安全一些。json.loads(astr1)也有效。In [25]: alist= eval(astr1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;In [26]: alist&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;Out[26]:&nbsp;[[-9, [[4, 2], [5, 6], [8, 10]]],&nbsp;[-2, [[3, 6], [6, 1]]],&nbsp;[4, [[3, 6], [6, 1], [3, 6], [6, 1], [3, 6], [6, 1], [3, 6], [6, 1]]]]如果它必须是一个数组,请执行以下操作:In [27]: arr = np.array(alist)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;In [28]: arr&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;Out[28]:&nbsp;array([[-9, list([[4, 2], [5, 6], [8, 10]])],&nbsp; &nbsp; &nbsp; &nbsp;[-2, list([[3, 6], [6, 1]])],&nbsp; &nbsp; &nbsp; &nbsp;[4,&nbsp; &nbsp; &nbsp; &nbsp; list([[3, 6], [6, 1], [3, 6], [6, 1], [3, 6], [6, 1], [3, 6], [6, 1]])]],&nbsp; &nbsp; &nbsp; dtype=object)这是 (3,2)。 arr[:,0]是一个整数数组,但arr[:,1]是一个列表数组。genfromtxt默认情况下,尝试将输入转换为浮点数(它可以转换为nan)。它适用于csv- 一个整齐的字符串数量表,每行具有相同的列数。

守候你守候我

您可以使用Exportwith"JSON"或"PythonExpression"将数据导出到文件。其中任何一个都可以被 Python 直接读取。下面我用它ExportString来演示。和x =&nbsp;{&nbsp; {-9, {{4, 2}, {5, 6}, {8, 10}}},&nbsp; {-2, {{3, 6}, {6, 1}}},&nbsp; {4, {{3, 6}, {6, 1}, {3, 6}, {6, 1}, {3, 6}, {6, 1}, {3, 6}, {6, 1}}}&nbsp; };然后ExportString[x, "JSON", "Compact" -> True][[-9,[[4,2],[5,6],[8,10]]],[-2,[[3,6],[6,1]]],[4,[[3,6],[6,1],[3,6],[6,1],[3,6],[6,1],[3,6],[6,1]]]]或者ExportString[x, "PythonExpression"][[-9, [[4, 2], [5, 6], [8, 10]]], [-2, [[3, 6], [6, 1]]], [4, [[3, 6], [6, 1], [3, 6], [6, 1], [3, 6], [6, 1], [3, 6], [6, 1]]]]Wolfram 语言(又名 Mathematica)可以导入和导出多种格式,上面两种是它支持的一些基本格式。通常不需要自定义输出的代码消耗,因为通常有其他平台可以天真地(或有库)读取的可用格式。希望这可以帮助。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python