我想在文本文件中编写以下代码的输出。它给出了这个错误:
for x in zip(c(), R1):
TypeError: zip argument #1 must support iteration
我找不到任何解决方案。有什么帮助吗?
import numpy as np
from math import *
from scipy.integrate import quad
from scipy.integrate import odeint
xx=np.array([0.01,0.012])
yy=np.array([32.95388698,33.87900347])
Cov=[[137,168],[28155,-2217]]
with open('txtfile.txt', 'w') as f:
for j in range (1,20):
R1=np.random.uniform(0,1)
Omn=0.32+R1
Odn=1-Omn
def dl(n):
fn=xx[n]*Odn+yy[n]*Omn
return fn
def c():
f_list = []
for i in range(2): #the value '2' reflects matrix size
f_list.append(dl(i))
r1=[f_list]
r2=[[f] for f in f_list]
a=np.dot(r1,Cov)
b=np.dot(a,r2)
matrix=np.linalg.det(b)
return matrix
for x in zip(c(), R1):
f.write("{0}\t{1}\n".format(x[0],x[1]))
感谢您的帮助。
答案 0 :(得分:1)
c()
和R1
都是简单值,而不是列表。因此,要将它们写入带有选项卡的文件,您只需要:
f.write("{}\t{}\n".format(c(), R1))
例如:
import numpy as np
from math import *
from scipy.integrate import quad
from scipy.integrate import odeint
def dl(n):
return xx[n] * Odn + yy[n] * Omn
def c():
f_list = []
for i in range(2): #the value '2' reflects matrix size
f_list.append(dl(i))
r1 = [f_list]
r2 = [[f] for f in f_list]
a = np.dot(r1, Cov)
b = np.dot(a, r2)
matrix = np.linalg.det(b)
return matrix
xx = np.array([0.01, 0.012])
yy = np.array([32.95388698, 33.87900347])
Cov = [[137, 168], [28155, -2217]]
with open('txtfile.txt', 'w') as f:
for j in range (1,20):
R1 = np.random.uniform(0, 1)
Omn = 0.32 + R1
Odn = 1 - Omn
f.write("{}\t{}\n".format(c(), R1))
这会创建您的txtfile.txt
,如下所示:
35206063.6746 0.777596199441
45374454.3839 0.926105934266
3990656.69091 0.0493187574204
28925205.8769 0.674852617966
45542873.2768 0.928417018276
4412088.81481 0.0683471360264
20148228.6097 0.510253466599
6934013.9475 0.166927414742
18602042.1473 0.477747802178
49485237.1146 0.981343401759
31379848.1448 0.716219179241
21670623.7641 0.541061316417
25859179.9751 0.620631842725
10642383.5164 0.28331967175
14640960.1091 0.387697186294
5183085.91921 0.100940240452
12734994.2117 0.340005554729
26863086.7454 0.638722906359
6227944.29448 0.141453730959
要为每一行编写额外的变量,我建议您切换到使用CSV编写器,如下所示:
import numpy as np
from math import *
from scipy.integrate import quad
from scipy.integrate import odeint
import csv
def dl(n):
return xx[n] * Odn + yy[n] * Omn
def c():
f_list = [dl(i) for i in range(2)]
r1 = [f_list]
r2 = [[f] for f in f_list]
a = np.dot(r1, Cov)
b = np.dot(a, r2)
matrix = np.linalg.det(b)
return matrix
xx = np.array([0.01, 0.012])
yy = np.array([32.95388698, 33.87900347])
Cov = [[137, 168], [28155, -2217]]
with open('txtfile.txt', 'w', newline='') as f:
csv_output = csv.writer(f, delimiter='\t')
for j in range (1,20):
R1 = np.random.uniform(0, 1)
Omn = 0.32 + R1
Odn = 1 - Omn
csv_output.writerow([c(), R1])
答案 1 :(得分:1)
zip函数的输入必须是* iterables这样的数组或列表。 请在下面试试,希望它会起作用。
for x in zip([c()], [R1]):
f.write("{0}\t{1}\n".format(x[0],x[1]))
python3中的zip文档可用here。
答案 2 :(得分:0)
当您使用zip()
时,您正在使用列表。
列表包含数组,您的函数和随机数只是没有[]
的数字,它演示了一个数组。所以你可以在没有包含zip()
的for循环的情况下使用。
f.write("{0}\t{1}\n".format(c(),R1))
另一点:从with open
订单中提取功能。