在 savetxt 中合并 python 个列表

combining python lists in savetxt

我有 2 个 python 列表,我想使用 np.savetxt() 并排保存它们。

我试过:

np.savetxt('./filename.txt',np.hstack([list1,list2]),fmt=['%f','%f'])

但我收到错误消息

raise AttributeError('fmt has wrong shape.  %s' % str(fmt))
AttributeError: fmt has wrong shape.  ['%f', '%f']

我不知道它是否相关,但列表是 decimal.Decimal 格式。

请问我做错了什么?


编辑:我最初说 "vstack" 但我的意思是 "hstack"。

只需将单个值传递给 fmt,如下所示:

np.savetxt('./filename.txt',np.vstack([list1,list2]),fmt='%f')

示例:

import decimal, numpy as np
a = np.array([decimal.Decimal("1.0"),
              decimal.Decimal("2.0"),
              decimal.Decimal("3.0")],
             dtype=np.dtype(decimal.Decimal))
b = a + 1
np.savetxt('./filename.txt',np.vstack([a, b]),fmt='%f')

生成的文件如下所示:

1.000000 2.000000 3.000000
2.000000 3.000000 4.000000