Python 如何在第二个函数中使用一个函数列表中的值

Python 如何在第二个函数中使用一个函数列表中的值,python,Python,我正在编写一个不确定的问题。我能够编写一个函数,并在python列表中获得x,y图。我希望能够在第二个函数中调用第一个函数的列表[0]和列表[1]。如何设置函数,以便将第一个函数的输出用作第二个函数的输入 import numpy as np import sympy as sy # find the point of intersection of two points by using a linear function def intersection(slope1, yinterce

我正在编写一个不确定的问题。我能够编写一个函数,并在python列表中获得x,y图。我希望能够在第二个函数中调用第一个函数的列表[0]和列表[1]。如何设置函数,以便将第一个函数的输出用作第二个函数的输入

import numpy as np
import sympy as sy

# find the point of intersection of two points by using a linear function

def intersection(slope1, yintercept1, slope2, yintercept2):
    xval = -1*slope1+slope2
    recip = np.reciprocal(xval)
    xval = yintercept1*recip
    yval = slope2*xval
    point = [xval, yval]
    return point
print(intersection((sy.Rational(-6,7)), 9, sy.Rational(7,6), 0))

#find the distance two points are apart.

def distance(x, y):

#I want to call the point from my intersection function here and put them into the distance formula 
to find out how far away the point is from the origin.  
 
print(distance(0,0))

函数intersection返回一个列表,所以将其保存到一个变量中,然后使用从intersection得到的这个变量调用距离函数

import math #for numeric sqrt you import math

def distance(p):
    x=p[0]
    y=p[1]
    return math.sqrt(x**2 + y**2) # euclidian distance to (0,0)

point = intersection((sy.Rational(-6,7)), 9, sy.Rational(7,6), 0))
d = distance(point)
将点作为元组返回,并在距离参数中解包

将numpy作为np导入 将sympy作为sy导入 def相交坡度1、yintercept1、坡度2、yintercept2: xval=-1*slope1+slope2 Recipe=np.reciprocalxval xval=yintercept1*recip yval=slope2*xval 返回xval,yval作为元组返回 def距离X,y: 做点什么 返回0 点=相交Y.Rational-6,7,9,sy.Rational 7,6,0 d=距离*点将点解包为参数 从技术上讲,你可以在一行中完成这一切,但像这样的一行读起来有点困难

d = distance(*intersection(sy.Rational(-6,7), 9, sy.Rational(7,6), 0))

我发现了我的问题。在我运行完函数后,我正在打印它,我需要将函数的值保存到一个变量中。一旦我将它保存到一个新变量中,我就可以在下一个函数中使用该变量。现在我遇到了一个处理有理数和使用symphy求平方根的问题

from numpy import reciprocal, sqrt
from sympy import Rational

'''Find the area of a right triangle by finding a point
where two perpendicular lines intersect, and by finding the distance
the point is away from the origin'''

# Enter the slope and y intercept for each of the perpendicular lines.  

slope_line_one = Rational(-6,7)
y_intercept_line_one = 9
slope_line_two = Rational(7,6)
y_intercept_line_two = 0
perpendicular_lines = [slope_line_one, y_intercept_line_one, slope_line_two, 
y_intercept_line_two]

# A function to find the (x,y) value where two perpendicular points intersect

def intersection(slope_one, y_intercept_one, slope_two, y_intercept_two):
    xval = -1*slope_one+slope_two
    recip = reciprocal(xval)
    xval = y_intercept_one*recip
    yval = slope_two*xval
    point = [xval, yval]
    return point
point_of_intersection = (intersection(perpendicular_lines[0], perpendicular_lines[1], 
perpendicular_lines[2], perpendicular_lines[3]))
print(point_of_intersection)

# This function is to find how far away the point made by the perpendicular lines is 
from the origin
origin = [0, 0]
def distance(x_two, x_one, y_two, y_one):
    xdistance = (x_two-x_one)**2
    return xdistance
length_leg_one = (distance(origin[0], point_of_intersection[0], origin[1], 
point_of_intersection[1]))
print(length_leg_one)
              

如果函数distance找到两点之间的距离,那么它的签名肯定应该是distancex1、y1、x2、y2?您可以全局使用declare points list,以便两个函数都可以访问它。将点作为元组返回,并在对distance的调用中解包。这不是最佳解决方案。python*操作符存在,因此您无需手动解压缩数据。至少它应该是x,y=p,p作为元组。但是它真的应该把x和y作为距离参数,并像这个distance*点那样调用它。我知道,但很明显这个人对编程非常陌生,所以我只是使用了基础知识,并重新定义了距离函数。在发布之前,我对第二个函数做了进一步的定义。我有两个解决方案,但每个都返回一个错误。我也会把错误放在这里。我认为我在第一个函数中有一个列表,可以在第二个函数中调用相同的点。可能是因为事情很简单?我必须等待,因为重新创建第二个函数需要一些时间。谢谢