简体   繁体   English

numpy数组的类似Python列表的字符串表示形式

[英]Python list-like string representation of numpy array

Consider there is a few rather long numpy arrays: 考虑有一些相当长的numpy数组:

importy numpy as np;
long_array1 = np.array([random.random() for i in range(10000)]);
long_array2 = np.array([random.random() for i in range(10000)]);
long_array3 = np.array([random.random() for i in range(10000)]);

I would like to save the arrays into the file file.dat , one row per numpy array. 我想将数组保存到文件file.dat ,每个numpy数组一行。 The text representation of an array should be in a python array-like format, ie in the case of following numpy array: 数组的文本表示应采用类似于python数组的格式,即在以下numpy数组的情况下:

a = np.array([0.3213,0.145323,0.852,0.723,0.421452])

I want to save following line in the file. 我想将以下行保存在文件中。

[0.3213,0.145323,0.852,0.723,0.421452]

There is what I do: 我在做什么:

array1_str = ",".join([str(item) for item in long_array1]);
array2_str = ",".join([str(item) for item in long_array2]);
array3_str = ",".join([str(item) for item in long_array3]);

with open("file.dat","w") as file_arrays:
    file_arrays.write("[" + array1_str + "]\n");
    file_arrays.write("[" + array2_str + "]\n");
    file_arrays.write("[" + array3_str + "]\n");

Everything works fine actually. 实际上一切正常。 I am just doubtful about the efficiency of my code. 我只是对代码的效率感到怀疑。 I am almost sure there has to be another (better and more efficient) way how to do this. 我几乎可以肯定,必须有另一种(更好,更高效)的方法来做到这一点。 I welcome comments to the random list generation as well. 我也欢迎对随机列表生成发表评论。

This is the fastest way: 这是最快的方法:

','.join(map(str, long_array1.tolist()))

If you want to keep the text more compact, this is fast too: 如果要使文本更紧凑,这也很快:

','.join(map(lambda x: '%.7g' % x, long_array1.tolist()))

Source: I benchmarked every possible method for this as the maintainer of the pycollada library. 资料来源:我以此为基准对pycollada库的维护者进行了测试。

Since you want a Python-list-like format, how about actually using the Python list format? 由于您希望使用类似Python列表格式的格式,因此如何实际使用Python列表格式呢?

array1_str = repr(list(long_array1))

That's going to stay mostly in C-land and performance should be much better. 那将主要留在C-land,性能应该会好得多。

If you don't want the spaces, take 'em out after: 如果您不想使用空格,请在以下时间取出它们:

array1_str = repr(list(long_array1)).translate(None, " ")

Memory usage may be an issue, however. 但是,内存使用可能是一个问题。

sounds like you might be able to use the numpy.savetxt() for this; 听起来您可能可以为此使用numpy.savetxt()

something like: 就像是:

def dump_array(outfile, arraylike):
    outfile.write('[')
    numpy.savetxt(outfile, arraylike, newline=',', fmt="%s")
    outfile.write(']\n')

although i don't think the corresponding numpy.loadtxt() will be able to read in this format. 虽然我不认为相应的numpy.loadtxt()能够以这种格式读取。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM