Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中计算多个坡度_Python_Loops_Permutation - Fatal编程技术网

在Python中计算多个坡度

在Python中计算多个坡度,python,loops,permutation,Python,Loops,Permutation,我想计算所有坐标(x1,y1)和(x2,y2)在所有可能组合中的斜率,而不重复任何步骤。有没有办法自动完成 #parameters x1 = 10.18182 x2 = 13.14286 y1= (0.30097,0.31036,0.3911,0.34255,0.198374,0.398574,0.377364,0.2428,0.319312,0.13338,0.228027,0.242637,0.326533) y2= (0.536551,0.450893,0.299292,0.286737,

我想计算所有坐标(x1,y1)和(x2,y2)在所有可能组合中的斜率,而不重复任何步骤。有没有办法自动完成

#parameters
x1 = 10.18182
x2 = 13.14286
y1= (0.30097,0.31036,0.3911,0.34255,0.198374,0.398574,0.377364,0.2428,0.319312,0.13338,0.228027,0.242637,0.326533)
y2= (0.536551,0.450893,0.299292,0.286737,0.438004,0.272566,0.42541,0.430583,0.282882,0.285569,0.286261,0.439658)

#Slope
m0=(y2[0]-y1[0])/(x2-x1)
m1=(y2[1]-y1[0])/(x2-x1) 
m2=(y2[2]-y1[0])/(x2-x1)
...
mn=(y2[11]-y1[12])/(x2-x1)

谢谢

单行代码:
m_list=[(y2_val-y1_val)/(x2-x1)对于y1中的y1_val对于y2中的y2_val]

x1 = 10.18182
x2 = 13.14286
y1= (0.30097,0.31036,0.3911,0.34255,0.198374,0.398574,0.377364,0.2428,0.319312,0.13338,0.228027,0.242637,0.326533)
y2= (0.536551,0.450893,0.299292,0.286737,0.438004,0.272566,0.42541,0.430583,0.282882,0.285569,0.286261,0.439658)


def slope(x1, y1, x2, y2):
        return (y2-y1)/(x2-x1)

    for y in y1:
        for y_ in y2:
            print(x1,y,x2,y_)
x1 = 10.18182
x2 = 13.14286
y1 = (0.30097,0.31036,0.3911,0.34255,0.198374,0.398574,0.377364,0.2428,0.319312,0.13338,0.228027,0.242637,0.326533)
y2 = (0.536551,0.450893,0.299292,0.286737,0.438004,0.272566,0.42541,0.430583,0.282882,0.285569,0.286261,0.439658)
m_list = [(y2_val-y1_val)/(x2-x1) for y1_val in y1 for y2_val in y2]
print m_list

使用
itertools.product

import itertools

yourlist=[(s2-s1)/(x2-x1) for s2,s1 in itertools.product(y2,y1)]

itertools.compositions