python – 更高效的代码(看似太多if语句)

python – 更高效的代码(看似太多if语句),第1张

概述我已经调查了这个,但我找不到任何帮助我的东西(道歉,如果类似的东西的答案可以帮助我).我正在编写一个货币转换器,它遭受了大量的if,它看起来效率不高,我也无法想象它的可读性非常好,所以我想知道如何在这种情况下编写更有效的代码: prompt = input("Input") #For currency, inputs should be written like "C(NUMBER)(CURREN 我已经调查了这个,但我找不到任何帮助我的东西(道歉,如果类似的东西的答案可以帮助我).我正在编写一个货币转换器,它遭受了大量的if,它看起来效率不高,我也无法想象它的可读性非常好,所以我想知道如何在这种情况下编写更有效的代码:

prompt = input("input") #For currency,inputs should be written like "C(NUMBER)(CURRENCY TO CONVERT FROM)(CURRENCY TO CONVERT TO)" example "C1cpsP"if prompt[0] == "C": #Looks at first letter and sees if it's "C". C = Currency Conversion    #CP = copper PIEce,SP = Silver PIEce,EP = Electrum PIEce,GP = Gold PIEce,PP = Platinum PIEce    ccint = int(''.join(List(filter(str.isdigit,prompt)))) # Converts Prompt to integer(Return string joined by str.(Filters out parameter(Gets digits (?),from prompt))))    ccAlpha = str(''.join(List(filter(str.isAlpha,prompt)))) #Does the same thing as above expect with letters    if ccAlpha[1] == "C": #C as in start of CP        acp = [ccint,ccint/10,ccint/50,ccint/100,ccint/1000] #Array of conversions. CP,SP,EP,GP,PP        if ccAlpha[3] == "C": #C as in start of CP            print(acp[0]) #Prints out corresponding array conversion        if ccAlpha[3] == "S": #S as in start of SP,ETC. ETC.            print(acp[1])        if ccAlpha[3] == "E":            print(acp[2])        if ccAlpha[3] == "G":            print(acp[3])        if ccAlpha[3] == "P":            print(acp[4])    if ccAlpha[1] == "S":        asp = [ccint*10,ccint,ccint/100]        if ccAlpha[3] == "C":            print(asp[0])        if ccAlpha[3] == "S":            print(asp[1])        if ccAlpha[3] == "E":            print(asp[2])        if ccAlpha[3] == "G":            print(asp[3])        if ccAlpha[3] == "P":            print(asp[4])    if ccAlpha[1] == "E":        aep = [ccint*50,ccint*5,ccint/2,ccint/20]        if ccAlpha[3] == "C":            print(aep[0])        if ccAlpha[3] == "S":            print(aep[1])        if ccAlpha[3] == "E":            print(aep[2])        if ccAlpha[3] == "G":            print(aep[3])        if ccAlpha[3] == "P":            print(aep[4])    if ccAlpha[1] == "G":        agp = [ccint*100,ccint*10,ccint*2,ccint/10]        if ccAlpha[3] == "C":            print(agp[0])        if ccAlpha[3] == "S":            print(agp[1])        if ccAlpha[3] == "E":            print(agp[2])        if ccAlpha[3] == "G":            print(agp[3])        if ccAlpha[3] == "P":            print(agp[4])    if ccAlpha[1] == "P":        app = [ccint*1000,ccint*100,ccint*20,ccint]        if ccAlpha[3] == "C":            print(app[0])        if ccAlpha[3] == "S":            print(app[1])        if ccAlpha[3] == "E":            print(app[2])        if ccAlpha[3] == "G":            print(app[3])        if ccAlpha[3] == "P":            print(app[4])
解决方法 您始终可以使用词典进行查找:

lookup = {'C': {'C': ccint,'S': ccint/10,'E': ccint/50,'G': ccint/100,'P': ccint/1000},'S': {'C': ccint*10,'S': ccint,'E': ccint/10,'G': ccint/10,'P': ccint/100},'E': {'C': ccint*50,'S': ccint*5,'E': ccint,'G': ccint/2,'P': ccint/20},'G': {'C': ccint*100,'S': ccint*10,'E': ccint*2,'G': ccint,'P': ccint/10},'P': {'C': ccint*1000,'S': ccint*100,'E': ccint*20,'G': ccint*10,'P': ccint}         }

那么你所有的ifs大部分都包含在:

print(lookup[ccAlpha[1]][ccAlpha[3]])

但是可能包含其他字符吗?然后你需要引入一个后备:

try:    print(lookup[ccAlpha[1]][ccAlpha[3]])except KeyError:    # Failed to find an entry for the characters:    print(ccAlpha[1],ccAlpha[3],"combination wasn't found")

如上所述,它不是最有效的方式,因为它每次都计算每次转换(即使是不必要的转换).拥有基线可能更有效,例如P并保存因子:

lookup = {'C': 1000,'S': 100,'E': 50,'G': 10,'P': 1,}# I hope I have them the right way around... :-)print(ccint * lookup[ccAlpha[3]] / lookup[ccAlpha[1]])
总结

以上是内存溢出为你收集整理的python – 更高效的代码(看似太多if语句)全部内容,希望文章能够帮你解决python – 更高效的代码(看似太多if语句)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1194307.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-03
下一篇 2022-06-03

发表评论

登录后才能评论

评论列表(0条)

保存