简体   繁体   English

numpy数组元素系列的格式化字符串

[英]formatted string of series of numpy array elements

It seems quit trivial to me but I'm still missing an efficient and "clean" way to insert a series of element belonging to numpy array (as aa[:,:] ) in a formatted string to be printed/written. 它似乎对我来说微不足道但我仍然缺少一种有效且“干净”的方式来插入属于numpy数组的一系列元素(如aa[:,:] )在要打印/写入的格式化字符串中。 In fact the extended element-by-element specification syntaxes like: 事实上,扩展的逐元素规范语法如下:

formattedline= '%10.6f  %10.6f  %10.6f' % (aa[ii,0], aa[ii,1], aa[ii,2]) 
file1.write(formattedline+'\n')

are working. 在工作中。

But I have not found any other shorter solution, because: 但我还没有找到任何其他更短的解决方案,因为:

formattedline= '%10.6f  %10.6f  %10.6f' % (float(aa[ii,:]))
file1.write(formattedline+'\n')

of course gives: TypeError: only length-1 arrays can be converted to Python scalars 当然给出: TypeError: only length-1 arrays can be converted to Python scalars

or: 要么:

formattedline= '%10.6f  %10.6f  %10.6f' % (aa[ii,:]) 
file1.write(formattedline+'\n')

gives: TypeError: float argument required, not numpy.ndarray . TypeError: float argument required, not numpy.ndarrayTypeError: float argument required, not numpy.ndarray I have tried with iterators but with no success. 我尝试过迭代器,但没有成功。

Of course this is interesting when there are several elements to be printed. 当然,当要打印几个元素时,这很有趣。

So: how can I combine iteration over numpy array and string formatted fashion? 那么:我怎样才能将迭代结合在numpy数组和字符串格式的时尚上?

You could convert it to a tuple: 你可以将它转换为元组:

formattedline = '%10.6f  %10.6f  %10.6f' % ( tuple(aa[ii,:]) )

In a more general case you could use a join : 在更一般的情况下,您可以使用join

formattedline = ' '.join('%10.6f'%F for F in aa[ii,:] )

If you are writing the entire array to a file, use np.savetxt: 如果要将整个数组写入文件,请使用np.savetxt:

np.savetxt(file1, aa, fmt = '%10.6f')

The fmt parameter can be a single format, or a sequence of formats, or a multi-format string like fmt参数可以是单个格式,也可以是格式序列,也可以是多格式字符串

'%10.6f  %5.6f  %d'

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

相关问题 将字符串格式、图像和 Label 的数据转换为列表 / numpy 数组 - Converting Data of string-formatted, Image and Label to list / numpy array 在numpy数组中每隔n次删除一系列元素 - Delete a series of elements every nth time in numpy array 将相同的字符串连接到数组或系列的所有元素 - concatenate same string to all elements of array or Series 根据字符串元素的字符对 Numpy 数组进行切片 - Slicing a Numpy array based on characters of string elements 无法将字符串元素分配给 NumPy 数组? - Cannot assign string elements to NumPy array? 将Pandas系列对象的所有格式不一致的字符串元素强制转换为datetime.time类型 - Coerce all inconsistently formatted string elements of Pandas series object into type datetime.time 如何测试熊猫系列是否包含来自另一个列表(或 NumPy 数组或熊猫系列)的元素? - How to test whether a pandas series contains elements from another list (or NumPy array or pandas series)? 使用复杂和/或真实元素打印格式良好的numpy数组(real_if_close) - Print nicely formatted numpy array with complex and / or real elements (real_if_close) numpy 数组的快速格式化文件输出 - fast formatted file output of numpy array 如何将系列转换为 numpy 阵列? - How to covert a series into a numpy array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM