Home > Blockchain >  python create function that takes several inputs
python create function that takes several inputs

Time:04-21

this code generates the function that results from the points taken from two lists. The points on l1 inputted in this function, generate the points in l2. What I want to do is to write a code that generates a function which takes multiple lists (inputs) and generate the same output but I am not sure how to modify this code to obtain what I want.

def get_equation(x,y):
    degree = 1
    coefs, res, _,_, _ = np.polyfit(x,y,degree, full = True)
    ffit = np.poly1d(coefs)
    print (ffit)
    return ffit
l1=[1,2,3,4]
l2=[2,4,6,8]
eq_list=(get_equation(l1,l2))

for i in (l1):
    print(round(eq_list(i)),'sadf',i)

CodePudding user response:

If I understand you correctly, you want to loop through a list of inputs and get the equation. See if the below helps you.

import numpy as np

def get_equation(x,y):
    degree = 1
    coefs, res, _,_, _ = np.polyfit(x,y,degree, full = True)
    ffit = np.poly1d(coefs)
    #print (ffit)
    return ffit

list_pairs = [ [[1,2,3,4], [2,4,6,8]],
               [[2,3,4,5], [4,6,8,10]],
               [[3,4,5,6], [6,8,10,12]],
               [[4,5,6,7], [8,10,12,14]] ]

for pair in list_pairs:
    print(get_equation(pair[0],pair[1]))

Output:

2 x   1.651e-15
 
2 x   2.581e-15
 
2 x   3.149e-15
 
2 x - 2.141e-14

CodePudding user response:

def takelist():
    mylist=list(map(int,input('list input space seperated ').split()))
    return mylist
n=int(input('number of list to input '))
for j in range(n):
    print(takelist())  

output=
number of list to input 2
list input space seperated 1 2 34
[1, 2, 34]
list input space seperated 1 2 34 5
[1, 2, 34, 5]

  • Related