簡體   English   中英

在savetxt中合並python列表

[英]combining python lists in savetxt

我有2個要使用np.savetxt()並排保存的python列表。

我努力了:

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']

我不知道它是否相關,但是列表是十進制格式。

請問我做錯了什么?


編輯:我最初說“ 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM