d =[]
for i in range(len(X_test)):
d.append(X_test[i], y_test[i], y_pred[i])
print(X_test[i]," ", y_test[i], " ", y_pred[i])
print(d)
输出
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Input In [33], in <cell line: 2>()
1 d =[]
2 for i in range(len(X_test)):
----> 3 d.append(X_test[i], y_test[i], y_pred[i])
4 print(X_test[i]," ", y_test[i], " ", y_pred[i])
6 print(d)
TypeError: list.append() takes exactly one argument (3 given)
如果我只是输出而不附加,这就是我得到的:
d =[]
for i in range(len(X_test)):
print(X_test[i]," ", y_test[i], " ", y_pred[i])
print(d)
输出
---------------------------------------------------------------------------
[1.5] 37731.0 40835.10590871474
[10.3] 122391.0 123079.39940819163
[4.1] 57081.0 65134.556260832906
[3.9] 63218.0 63265.36777220843
[9.5] 116969.0 115602.64545369372
[8.7] 109431.0 108125.89149919583
[9.6] 112635.0 116537.23969800597
[4.] 55794.0 64199.96201652067
[5.3] 83088.0 76349.68719257976
[7.9] 101302.0 100649.13754469794
所以这就是我要做的。我想要一个附加3个参数的列表。这样我的列表看起来就像:
[[1.5, 37731.0, 40835.10590871474]
[10.3, 122391.0, 123079.39940819163]
[4.1, 57081.0, 65134.556260832906]
[3.9, 63218.0, 63265.36777220843]
[9.5, 116969.0, 115602.64545369372]
[8.7, 109431.0, 108125.89149919583]
[9.6, 112635.0, 116537.23969800597]
[4.0, 55794.0, 64199.96201652067]
[5.3, 83088.0, 76349.68719257976]
[7.9, 101302.0, 100649.13754469794]]
看起来您想要一个列表的列表。试着把第3行改成
d.append([X_test[i], y_test[i], y_pred[i]])
将三个项目作为一个列表追加。
只能添加一个值。在本例中,您想要添加一个包含3个值的列表。
d.append([X_test[i], y_test[i], y_pred[i]])
如果你想一次附加3个单独的值,使用相同的列表作为extend
方法的参数。
>>> d = []
>>> d.extend([1,2,3])
>>> d
[1, 2, 3]
d = []
for i in range(len(X_test)):
e = []
e.append(X_test[i])
e.append(y_test[i])
e.append(y_pred[i])
d.append(e)
print(X_test[i]," ", y_test[i], " ", y_pred[i])
print(d)
Output :
1.5 37731.0 40835.10590871474
10.3 122391.0 123079.39940819163
4.1 57081.0 65134.556260832906
3.9 63218.0 63265.36777220843
[
[1.5, 37731.0, 40835.10590871474],
[10.3, 122391.0, 123079.39940819163],
[4.1, 57081.0, 65134.556260832906],
[3.9, 63218.0, 63265.36777220843]
]
我想你正在寻找这样的东西。
X_test = ['1.5', '10.3', '4.1']
y_test = ['37731.0', '122391.0', '57081.0' ]
y_pred = ['40835.10590871474','123079.39940819163','65134.556260832906']
d = list()
for i in range(len(X_test)):
if X_test[i] and y_test[i] and y_pred[i]:
d.append([X_test[i], y_test[i], y_pred[i]])
for item in d:
print(item)