Python 为循环执行此操作的其他方法?
我只是想知道是否有一种改进for循环的方法,通过某种方式跳过那些if VarPython 为循环执行此操作的其他方法?,python,string,for-loop,replace,split,Python,String,For Loop,Replace,Split,我只是想知道是否有一种改进for循环的方法,通过某种方式跳过那些if VarString可以有更多的参数,并且它们的顺序可以是随意的。 为了用实际值替换参数,我需要: 分开 检查参数是什么以及在哪个位置 下面是一个关于我如何思考的综合示例: String = "{FullDate}_{Month}_{Day}_{Year}_{ElementID}_{ElementCD}" String_split = String.split("_") for params in range(len(Stri
String
可以有更多的参数,并且它们的顺序可以是随意的。为了用实际值替换参数,我需要:
String = "{FullDate}_{Month}_{Day}_{Year}_{ElementID}_{ElementCD}"
String_split = String.split("_")
for params in range(len(String_split)):
if "FullDate" in String_split[params]:
# Do something
elif "Name" in String_split[params]:
# Do something
elif "ElementID" in String_split[params]:
# Do something
elif "ElementCD" in String_split[params]:
# Do something
elif "Year" in String_split[params]:
# Do something
elif "Day" in String_split[params]:
# Do something
elif "Month" in String_split[params]:
# Do something
更新:这就是我想要完成的
# Default values
FullDate = now().format("yyyy-MM-dd_HH:mm:ss")
Name = "John"
ElementID = "Apple"
ElementCD = "01Appxz"
Year = now().format("yyyy")
Day = now().format("dd")
Month = now().format("MM")
############################
String = "{FullDate}_{Month}_{Day}_{Year}_{ElementID}_{ElementCD}"
String_split = String.split("_")
for params in range(len(String_split)):
if "FullDate" in String_split[params]:
Report_Name = Report_Name + FullDate + "_"
elif "Name" in String_split[params]:
Report_Name = Report_Name + Name + "_"
elif "ElementID" in String_split[params]:
Report_Name = Report_Name + ElementID + "_"
elif "ElementCD" in String_split[params]:
Report_Name = Report_Name + ElementCD + "_"
elif "Year" in String_split[params]:
Report_Name = Report_Name + Year + "_"
elif "Day" in String_split[params]:
Report_Name = Report_Name + Day + "_"
elif "Month" in String_split[params]:
Report_Name = Report_Name + Month + "_"
# Report_Name must return default values, ordered by String variable (eg: FullDate, 1st position; Month 2nd position etc..)
# >> "1999-01-01_10:10:29_01_01_1999_Apple_01Appxz"
# if the String variable changes the params order to
# String = "{Year}_{Month}_{ElementCD}_{FullDate}_{ElementID}_{Day}"
# Report_Name should return
# >> "1999_01_01Appxz_1999-01-01_10:10:29_Apple_01"
阅读前:
-正如您提到的,这是一个不用字典的解决方案
解决方案 与: 您可以在不使用
for
循环的情况下执行此操作,只需根据需要更换:
Report_Name = '.RName_' + String
if "FullDate" in Report_Name:
Report_Name = Report_Name.replace('{FullDate}',FullDate)
if "Name" in Report_Name:
Report_Name = Report_Name.replace('{Name}',Name)
#...
if "ElementCD" in Report_Name:
Report_Name = Report_Name.replace('{ElementCD}',ElementCD)
print(Report_Name)
.RName_2010-01-01_00:00:00_..._01Appxz
[小心]另一种解决方案 或者,您可以使用
.eval()
(请参阅)从变量名称计算变量。它要求参数
和变量
名称相同。以下是一种方法:
import re
Parameters = [re.sub('[{-}]+', '', s) for s in String.split('_')]
Report_Name = '.RName_' + String
for p in Parameters:
Report_Name = Report_Name.replace('{%s}'%p,eval(p))
print(Report_Name)
.RName_2010-01-01_00:00:00_01_01_2010_Apple_01Appxz
请注意,您应该小心地使用.eval()
-检查此解决方案的替代方案-例如,如果:
- 你想要类似的行为
- 你认为这对你的问题来说不够安全吗
- 对名称中的名称使用
删除所有,
杂音字符串分割[参数]
- 从变量中删除
,以便可以使用{}
而不是=
中的
- 使用
+=
names = "FullDate Month Day Year ElementID ElementCD".split()
for name in names:
if "FullDate" == name:
Report_Name += FullDate + "_"
elif "Name" == name:
Report_Name += Name + "_"
elif "ElementID" == name:
Report_Name += ElementID + "_"
elif "ElementCD" == name:
Report_Name += ElementCD + "_"
elif "Year" == name:
Report_Name += Year + "_"
elif "Day" == name:
Report_Name += Day + "_"
elif "Month" == name:
Report_Name += Month + "_"
您还应该学习如何使用格式字符串和**
运算符。如果您将FullDate
内容更改为字典,则可以使用:
REPORT_FORMAT = '{FullDate}_{Month}_{Day}_{Year}_{ElementID}_{ElementCD}'
report = {
'FullDate': now().format("yyyy-MM-dd_HH:mm:ss")
'Name': "John"
'ElementID': "Apple"
'ElementCD': "01Appxz"
'Year': now().format("yyyy")
'Day': now().format("dd")
'Month': now().format("MM")
}
report_name = REPORT_FORMAT.format(**report)
一般来说,您的方法是可以的(Python没有
switch/case
语句,因此为此使用if/elif
或if/if
链完全是Python式的)。如果您的do something
语句非常相似(例如,总是使用具有不同参数的相同函数),那么您可以使用字典来进行切换,但可读性可能会受到影响。对于字符串分割中的参数,我会说:。但取决于“做点什么”意味着什么,可能会有更多的优化空间。优化可能存在,但你的方法是可以的。整体阅读这个问题会让我相信这是一个XY问题。你到底想达到什么目的?如果您只想用任意变量格式化字符串,那么有更好的方法。如果您有工作代码,希望在可读性、可维护性、兼容性和/或可加速性等方面进行改进,请继续询问。
REPORT_FORMAT = '{FullDate}_{Month}_{Day}_{Year}_{ElementID}_{ElementCD}'
report = {
'FullDate': now().format("yyyy-MM-dd_HH:mm:ss")
'Name': "John"
'ElementID': "Apple"
'ElementCD': "01Appxz"
'Year': now().format("yyyy")
'Day': now().format("dd")
'Month': now().format("MM")
}
report_name = REPORT_FORMAT.format(**report)