简体   繁体   中英

PYTHON: solving y=a+b*x where x is a predefined list

As mentioned above, I was trying to solve the problem... description: y is an empty list, each time the equation will be evaluated with y=a+bx where x is a list with no. of values in it, the evaluated answer for y will be added to the list named y (appended). Now my problem is that I have tried almost every google search in for this solution but no avail, I am a beginner in Python.

I do have the block of code which I tried according to my beginner knowledge it didn't work

ERROR_SHOWN: can't multiply sequence by non-int of type numpy.float64

hope it helps in any way...

code:

y = []
for num in x:
   y=a+b*x
append(y)
print(y)

hope the information I've provided is helpful

y = [a+b*num for num in x]
print(y)

I guess the following is the thing that you are trying to do.

y = []
for num in x:
   some_y = a+b*num
   y.append(some_y)
print(y)

Try this

x = [1,2,3,4]
y = []

a = 1
b = 2

for num in x:
    result = a+b*num
    y.append(result)
print(y)

here with numpy:

import numpy as np
x = np.array([1,2,3,4,5,6,7,8,9,10])
a, b= 10,5
y = a+b*x
y

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM