Home > Net >  I want a list to append more than one arguments. python
I want a list to append more than one arguments. python

Time:06-25

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)

This is output

---------------------------------------------------------------------------
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)

If I just the output without appending, This is what I get:

d =[]
for i in range(len(X_test)):
    print(X_test[i],"  ", y_test[i], " ", y_pred[i])

print(d)

This is the 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
[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

So this is what I am trying to do. I want a list to append 3 arguments. So that my list will look like:

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

CodePudding user response:

It looks like you want a list of lists. Try changing line 3 into

d.append([X_test[i], y_test[i], y_pred[i]])

to append the three items as a list.

CodePudding user response:

You can only append one value. In this case, you want to append one list of 3 values.

d.append([X_test[i], y_test[i], y_pred[i]])

If you wanted to append 3 separate values at once, use the same list as an argument to the extend method instead.

>>> d = []
>>> d.extend([1,2,3])
>>> d
[1, 2, 3]

CodePudding user response:

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

CodePudding user response:

I think you are looking for something like this.

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)
  • Related