从 python 数组编写一个 .gml(或便于 mathematica 读取)文件

writing a .gml (or easy for mathematica to read) file from python array

我在 python 中有一个相当大的数组(8000ish entires),它是图形的边缘列表。数组的一部分看起来像:

... [' Tulane-B (Tulane (Bacchus)) ', ' Florida Tech (Florida Tech) '],
[' Stanford-B (Stanford Prison Experiment) ', ' San Jose State (Megabyte) '],
[' Stanford-B (Stanford Prison Experiment) ', ' Duke (Brimstone) '],
[' Stanford-B (Stanford Prison Experiment) ', ' Oregon State (Beavers) '],...

字符串对顶点对,它们之间有一条边。

有没有一种有效的方法可以将这个数组导入到 Mathematica 中,以便它可以读取它?我知道 Mathematica 字符串被 { 包围。我可以仅以 python 以外的不同格式导出此数组,还是必须将其重新解析为字符串? 我的代码看起来像:

print('{', end = '')
for edge in named_edgelist: 
        print('{' + '\"' + edge[0] +'\"' +  ", " +'\"'  + edge[1] + '\"' + '},', end ='')
print('}',end = '')

倒数第二个 print 调用多了一个逗号,但我不担心这个。

提前致谢。

由于您的数据是干净的,即它不包含 double-quotes 或方括号,我们可以使用 str.translate 方法将列表的 Python 的本机字符串表示形式转换为Mathematica 格式。

要使用 str.translate,我们首先需要使用 str.maketrans 方法构建翻译 table。

data = [
    [' Tulane-B (Tulane (Bacchus)) ', ' Florida Tech (Florida Tech) '],
    [' Stanford-B (Stanford Prison Experiment) ', ' San Jose State (Megabyte) '],
    [' Stanford-B (Stanford Prison Experiment) ', ' Duke (Brimstone) '],
    [' Stanford-B (Stanford Prison Experiment) ', ' Oregon State (Beavers) '],
]

trans = str.maketrans("'[]", '"{}')
s = str(data).translate(trans)
print(s)

输出

{{" Tulane-B (Tulane (Bacchus)) ", " Florida Tech (Florida Tech) "}, {" Stanford-B (Stanford Prison Experiment) ", " San Jose State (Megabyte) "}, {" Stanford-B (Stanford Prison Experiment) ", " Duke (Brimstone) "}, {" Stanford-B (Stanford Prison Experiment) ", " Oregon State (Beavers) "}}

这比使用具有 Python 字符串格式的 Python 循环更快,因为几乎所有工作都是由用 C 编写的代码完成的,因此它以 C 速度运行。


FWIW,这也可以在 Python 2 中完成,只需稍作改动。在 Python 2 中,没有 str.maketrans 方法。相反,您需要使用 string.maketrans 辅助函数。


如果您希望每一行都在单独的一行上,则需要 pre-process 每行。这肯定会慢一点,但是对于 8000 行,速度差异几乎不明显。

data = [
    [' Tulane-B (Tulane (Bacchus)) ', ' Florida Tech (Florida Tech) '],
    [' Stanford-B (Stanford Prison Experiment) ', ' San Jose State (Megabyte) '],
    [' Stanford-B (Stanford Prison Experiment) ', ' Duke (Brimstone) '],
    [' Stanford-B (Stanford Prison Experiment) ', ' Oregon State (Beavers) '],
]

# Convert data to strings with each row on a separate line
s = '[\n    ' + ',\n    '.join([str(row) for row in data]) + '\n]'

trans = str.maketrans("'[]", '"{}')
s = s.translate(trans)
print(s)

输出

{
    {" Tulane-B (Tulane (Bacchus)) ", " Florida Tech (Florida Tech) "},
    {" Stanford-B (Stanford Prison Experiment) ", " San Jose State (Megabyte) "},
    {" Stanford-B (Stanford Prison Experiment) ", " Duke (Brimstone) "},
    {" Stanford-B (Stanford Prison Experiment) ", " Oregon State (Beavers) "}
}