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_Variables - Fatal编程技术网

Python 尝试创建循环以将变量从一个数组添加到另一个数组

Python 尝试创建循环以将变量从一个数组添加到另一个数组,python,loops,variables,Python,Loops,Variables,这是一个100%的超级noob问题(我刚刚开始使用python)。 我正在尝试打印我在一个名为“variables”的数组中放入的所有变量值,我想将其添加到名为“names”的字符串数组中 基本上,我试图用x=“it's number”、t=“it's number”等打印所有变量,并尝试在垂直列中进行打印 任何帮助都将不胜感激 x = np.array([1,3,4]) t = np.array([4,6,10]) X = sum(x) # X = 8 T

这是一个100%的超级noob问题(我刚刚开始使用python)。 我正在尝试打印我在一个名为“variables”的数组中放入的所有变量值,我想将其添加到名为“names”的字符串数组中 基本上,我试图用x=“it's number”、t=“it's number”等打印所有变量,并尝试在垂直列中进行打印

任何帮助都将不胜感激

x = np.array([1,3,4])
t = np.array([4,6,10])
X = sum(x)                   # X = 8
T = sum(t)                   # T = 20
S = sum(x **2)               # S = 26
P = sum(x*t)                 # P = 62
D = ((3*S) - (X **2))        # D = 14
m = ((3*P) - (X*T))/(D)      # m = 1.86
c = ((T*S) - (X*P))/(D)      # c = 1.71
e0 = t[0] - (m * x[0]) - c   # e0 = 0.429
e1 = t[1] - (m * x[1]) - c   # e1 = -1.29
e2 = t[2] - (m * x[2]) - c   # e2 = 0.857
E = (e0 ** 2) + (e1 ** 2) + (e2 ** 2)       # e= 2.57
dm = math.sqrt((3 * E)/ ((3 * S) - (X ** 2)))   # dm = 0.742
dc = math.sqrt((S * E)/((3 * S) - (X ** 2)))    # dc = 2.19
names = ['x = ',  't =',  'X =', 'T =', 'S =', 'P =', 'D =', 'm =', 'c =', 'e0 =', 'e1 =', 'e2 =', 'E =']
variables = [x, t, X, T, S, P, D, m, c, e0, e1, e2, E, dm, dc]
print(names[0], variables[0])*
    
使用:


这就是如何在python中执行循环的方法

将列表中的项与项进行匹配是我们的工作

在这里,您可以执行以下操作:

names = ['x = ',  't =',  'X =', 'T =', 'S =', 'P =', 'D =', 'm =', 'c =', 'e0 =', 'e1 =', 'e2 =', 'E =']
variables = [x, t, X, T, S, P, D, m, c, e0, e1, e2, E, dm, dc]

for name, value in zip(names, variables):
    print(f'{name} {value}')
这将给你:

x =  [1 3 4]
t = [ 4  6 10]
X = 8
T = 20
S = 26
P = 62
D = 14
m = 1.8571428571428572
c = 1.7142857142857142
e0 = 0.4285714285714286
e1 = -1.2857142857142854
e2 = 0.857142857142857
E = 2.5714285714285703
您也可以用同样的方法创建字典,这可能是管理所有这些值的更简单的方法:

dict(zip(names, variables))

{'x': array([1, 3, 4]),
 't': array([ 4,  6, 10]),
 'X': 8,
 'T': 20,
 'S': 26,
 'P': 62,
 'D': 14,
 'm': 1.8571428571428572,
 'c': 1.7142857142857142,
 'e0': 0.4285714285714286,
 'e1': -1.2857142857142854,
 'e2': 0.857142857142857,
 'E': 2.5714285714285703}

啊,好吧,这是有道理的。非常感谢你的帮助!
dict(zip(names, variables))

{'x': array([1, 3, 4]),
 't': array([ 4,  6, 10]),
 'X': 8,
 'T': 20,
 'S': 26,
 'P': 62,
 'D': 14,
 'm': 1.8571428571428572,
 'c': 1.7142857142857142,
 'e0': 0.4285714285714286,
 'e1': -1.2857142857142854,
 'e2': 0.857142857142857,
 'E': 2.5714285714285703}