使用列表创建方程式

时间:2019-05-23 05:15:31

标签: python

我有以下代码,它生成多个列表,如下所示:

for i in range(0, 10):
    diff = []
    for j in range(10):
        diff.append(i*j)
    print diff

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
[0, 3, 6, 9, 12, 15, 18, 21, 24, 27]
[0, 4, 8, 12, 16, 20, 24, 28, 32, 36]
[0, 5, 10, 15, 20, 25, 30, 35, 40, 45]
[0, 6, 12, 18, 24, 30, 36, 42, 48, 54]
[0, 7, 14, 21, 28, 35, 42, 49, 56, 63]
[0, 8, 16, 24, 32, 40, 48, 56, 64, 72]
[0, 9, 18, 27, 36, 45, 54, 63, 72, 81]

我想为每一列生成1个等式。

例如:

(element0_list1) * a + (element0_list2) * b + (element0_list3) * c + (element0_list4) * d + (element0_list5) * e + (element0_list6) * f + (element0_list7) * g + (element0_list8) * h + (element0_list9) * i = X 

因此,如果我从每一行中选择第二个元素,则示例公式为:

0 * a + 1 * b + 2 * c + 3 * d + 4 * e + 5 * f + 6 * g + 7 * h + 8 * i = X

有没有一种有效的方法可以在Python中完成并生成此类方程式?

我尝试使用多维数组,但无法正常工作。

3 个答案:

答案 0 :(得分:0)

一种打印列方式而不是行方式的方法如下,我们首先创建列表,然后使用双for循环对其进行迭代,然后在内部for循环中创建方程式并进行打印在外部for循环中

#Generate the 2D list
diff = []
for i in range(0, 10):
    li = []
    for j in range(10):
        li.append(i*j)
    diff.append(li)

#List of variables
variables = ["a","b","c","d","e","f","g","h", "i", "j"]

#Double for loop iteration
for i in range(len(diff)):
    s = []
    #Create the equation in the inner loop
    for j in range(len(diff[0])):
        s.append('{} * {}'.format(diff[j][i], variables[j]))

    #Create the equation string in the outer for loop and print it
    eq = ' + '.join(s) + ' = X'
    print(eq)

输出将为

0 * a + 0 * b + 0 * c + 0 * d + 0 * e + 0 * f + 0 * g + 0 * h + 0 * i + 0 * j = X
0 * a + 1 * b + 2 * c + 3 * d + 4 * e + 5 * f + 6 * g + 7 * h + 8 * i + 9 * j = X
0 * a + 2 * b + 4 * c + 6 * d + 8 * e + 10 * f + 12 * g + 14 * h + 16 * i + 18 * j = X
0 * a + 3 * b + 6 * c + 9 * d + 12 * e + 15 * f + 18 * g + 21 * h + 24 * i + 27 * j = X
0 * a + 4 * b + 8 * c + 12 * d + 16 * e + 20 * f + 24 * g + 28 * h + 32 * i + 36 * j = X
0 * a + 5 * b + 10 * c + 15 * d + 20 * e + 25 * f + 30 * g + 35 * h + 40 * i + 45 * j = X
0 * a + 6 * b + 12 * c + 18 * d + 24 * e + 30 * f + 36 * g + 42 * h + 48 * i + 54 * j = X
0 * a + 7 * b + 14 * c + 21 * d + 28 * e + 35 * f + 42 * g + 49 * h + 56 * i + 63 * j = X
0 * a + 8 * b + 16 * c + 24 * d + 32 * e + 40 * f + 48 * g + 56 * h + 64 * i + 72 * j = X
0 * a + 9 * b + 18 * c + 27 * d + 36 * e + 45 * f + 54 * g + 63 * h + 72 * i + 81 * j = X

答案 1 :(得分:0)

这是您要寻找的吗?

import numpy as np
arr = np.asarray([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18],
[0, 3, 6, 9, 12, 15, 18, 21, 24, 27],
[0, 4, 8, 12, 16, 20, 24, 28, 32, 36],
[0, 5, 10, 15, 20, 25, 30, 35, 40, 45],
[0, 6, 12, 18, 24, 30, 36, 42, 48, 54],
[0, 7, 14, 21, 28, 35, 42, 49, 56, 63],
[0, 8, 16, 24, 32, 40, 48, 56, 64, 72],
[0, 9, 18, 27, 36, 45, 54, 63, 72, 81]])

coefficients = ["a","b","c","d","e","f","g","h", "i"]
element_index = 1
equation = ' + '.join([str(i)+"*{}".format(j) for i,j in zip(arr[element_index, :], coefficients)])

输出:

'0*a + 1*b + 2*c + 3*d + 4*e + 5*f + 6*g + 7*h + 8*i'

答案 2 :(得分:0)

您要打印方程式吗?试试这个:

for i in range(10):
    row = []
    for j in range(10):
        row.append(i*j)
    print(f'{row[0]} * a + {row[1]} * b + {row[2]} * c + {row[3]} * d + {row[4]} * e + {row[5]} * f + {row[6]} * g + {row[7]} * h + {row[8]} * i = X')

输出

0 * a + 0 * b + 0 * c + 0 * d + 0 * e + 0 * f + 0 * g + 0 * h + 0 * i = X
0 * a + 1 * b + 2 * c + 3 * d + 4 * e + 5 * f + 6 * g + 7 * h + 8 * i = X
0 * a + 2 * b + 4 * c + 6 * d + 8 * e + 10 * f + 12 * g + 14 * h + 16 * i = X
0 * a + 3 * b + 6 * c + 9 * d + 12 * e + 15 * f + 18 * g + 21 * h + 24 * i = X
0 * a + 4 * b + 8 * c + 12 * d + 16 * e + 20 * f + 24 * g + 28 * h + 32 * i = X
0 * a + 5 * b + 10 * c + 15 * d + 20 * e + 25 * f + 30 * g + 35 * h + 40 * i = X
0 * a + 6 * b + 12 * c + 18 * d + 24 * e + 30 * f + 36 * g + 42 * h + 48 * i = X
0 * a + 7 * b + 14 * c + 21 * d + 28 * e + 35 * f + 42 * g + 49 * h + 56 * i = X
0 * a + 8 * b + 16 * c + 24 * d + 32 * e + 40 * f + 48 * g + 56 * h + 64 * i = X
0 * a + 9 * b + 18 * c + 27 * d + 36 * e + 45 * f + 54 * g + 63 * h + 72 * i = X