def get_resources(*required):
"""
This is to help building functions in the main calculator faster
returns lists of resources needed, in the order requested
"""
print('-'*30)
a = ['acceleration', 0, 'a', 'm s⁻²', '.2f', 'scalar']
A = ['amplitude', 0, 'A', 'm', '.2f', 'scalar'] # A: amplitude
Em = ['combined mass', 0, '∑m', 'kg', '.2f', 'scalar']
f = ['frequency', 0, 'f', 'Hz', '.1f', 'scalar'] # f: frequency (also m s⁻¹)
F = ['force', 0, 'F', 'N', '.2f', 'scalar']
g = ['gravity', 9.81, 'a', 'm s⁻²', '.2f', 'scalar']
hl = ['wavelength', 0, 'λ', 'm', '.2f','vector'] # λ: wavelength
m = ['mass', 0, 'm', 'kg', '.2f', 'scalar']
m1 = ['mass1', 0, 'm1', 'kg', '.2f', 'scalar']
m2 = ['mass2', 0, 'm2', 'kg', '.2f', 'scalar']
t = ['time', 0, 't', 's', '.2f', 'scalar']
T = ['tension', 0, 'T', 'N', '.1f', 'scalar']
T = ['period', 0, 'T', 's', '.3f', 'scalar'] # T: Time PERIOD of oscillation in seconds (T = 2π√(m/k))
v = ['wave speed', 0, 'v', 'm s⁻¹', '.2f', 'scalar'] # v: speed of a wave (v = fλ OR v = λ/T)
v = ['velocity', 0, 'v', 'm s⁻¹', '.2f', 'scalar']
v0 = ['initial velocity', 0, 'v₀', 'm s⁻¹', '.2f', 'vector']
vf = ['final velocity', 0, 'vf', 'm s⁻¹', '.2f', 'vector']
x = ['change in distance', 0, 'Δx', 'm', '.2f', 'scalar']
x = ['distance', 0, 'x', 'm', '.2f', 'scalar']
for i in required:
for j in vars().items():
if i == j[0]: print('{} = {}'.format(j[0], j[1]))
我正在尝试编写一个函数(接受参数),然后将它们与函数中的值进行比较,并打印所有可能的匹配项。但是,它不会打印所有可能的匹配项,只会打印与名称匹配的最终匹配项。另外我想在列表[1]位置使用字母字符,例如x = ['距离',0,' x',' m', ' .2f','标量']> x = ['距离',ABC,' x',' m',' .2f','标量' ]
目前如果我这样做
get_resources('x','F', 'T')
会打印
------------------------------
x = ['distance', 0, 'x', 'm', '.2f', 'scalar']
F = ['force', 0, 'F', 'N', '.2f', 'scalar']
T = ['period', 0, 'T', 's', '.3f', 'scalar']
但我想像这样打印。
------------------------------
x = ['change in distance', xc, 'Δx', 'm', '.2f', 'scalar']
x = ['distance', xc, 'x', 'm', '.2f', 'scalar']
F = ['force', Fc, 'F', 'N', '.2f', 'scalar']
T = ['tension', Tc, 'T', 'N', '.1f', 'scalar']
T = ['period', Tc, 'T', 's', '.3f', 'scalar']
如何重写此函数以根据需要返回结果?
答案 0 :(得分:0)
这解决了这个问题,不幸的是我必须删除评论..啊好
def get_resources(*required):
"""
This is to help building functions in the main calculator faster
returns lists of resources needed, in the order requested
"""
print('-'*30)
longstring = \
"a = ['acceleration', ac, 'a', 'm s⁻²', '.2f', 'scalar'] | " \
"A = ['amplitude', 0, 'Ac', 'm', '.2f', 'scalar'] | " \
"Em = ['combined mass', Emc, '∑m', 'kg', '.2f', 'scalar'] | " \
"f = ['frequency', fc, 'f', 'Hz', '.1f', 'scalar'] | " \
"F = ['force', Fc, 'F', 'N', '.2f', 'scalar'] | " \
"g = ['gravity', 9.81, 'a', 'm s⁻²', '.2f', 'scalar'] | " \
"hl = ['wavelength', hlc, 'λ', 'm', '.2f', 'vector'] | " \
"KE = ['kinetic energy', KEc, 'KE', 'J', '.1f', 'scalar'] | " \
"m = ['mass', 0, 'mc', 'kg', '.2f', 'scalar'] | " \
"m1 = ['mass1', m1c, 'm1', 'kg', '.2f', 'scalar'] | " \
"m2 = ['mass2', m2c, 'm2', 'kg', '.2f', 'scalar'] | " \
"t = ['time', tc, 't', 's', '.2f', 'scalar'] | " \
"T = ['tension force', Tc, 'T', 'N', '.1f', 'scalar'] | " \
"T = ['period', Tc, 'T', 's', '.3f', 'scalar'] | "
"v = ['wave speed', vc, 'v', 'm s⁻¹', '.2f', 'scalar'] | " \
"v = ['velocity', vc, 'v', 'm s⁻¹', '.2f', 'scalar'] | " \
"v0 = ['initial velocity', v0c, 'v₀', 'm s⁻¹', '.2f', 'vector'] | " \
"vf = ['final velocity', vfc, 'vf', 'm s⁻¹', '.2f', 'vector'] | " \
"W = ['work', Wc, 'W','J', '.2f', 'scalar'] # W: work (W = Fd) | " \
"x = ['change in distance', xc, 'Δx', 'm', '.2f', 'scalar'] | " \
"x = ['distance', xc, 'x', 'm', '.2f', 'scalar'] | "
variables = (longstring.split('|'))
for i in required:
for j in variables:
k = j.split('=')
k[0] = k[0].strip()
if i == k[0]:
k[1] = k[1].strip()
print('{} = {}'.format(k[0], k[1]))
get_resources('m1', 'm2', 'T', 'a', 'g', 't', 'x', 'v0')
get_resources('m','v','KE','x','F', 'T')