Python创建功能,获取多个输入

发布于 2025-01-22 15:24:26 字数 440 浏览 0 评论 0原文

该代码生成了从两个列表中获取的点产生的功能。 l1在此功能中输入的点上,在l2中生成点。 我要做的是编写一个生成一个函数的代码,该函数获取多个列表(输入)并生成相同的输出,但我不确定如何修改此代码以获取我想要的东西。

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)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

提笔落墨 2025-01-29 15:24:26

如果我正确理解您,您想循环浏览输入列表并获取方程式。看看下面是否对您有帮助。

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

输出:

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

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
蓝礼 2025-01-29 15:24:26
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())  

输出=
输入2的列表
列表输入空间分开1 2 34
[1,2,34]
列表输入空间分离1 2 34 5
[1,2,34,5]

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]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文