![](/img/trans.png)
[英]If I append more than one of the same thing to a list and i want to get rid of one of them how do i know which one it is?
[英]I want a list to append more than one arguments. python
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.