如何在python的if语句内使用变量?例如,month_report
包含:
October
November
December
January
和cycle_report
包含:
October,Monthly
November,Monthly
November,Quarterly
January,Monthly
下面是我认为可以使用变量的方式。
mcount = 0
qcount = 0
ncount = 0
for month in month_report:
for x in cycle_report:
if x == "{month},Monthly":
mcount += 1
elif x == "{month},Quarterly":
qcount += 1
else:
ncount += 1
答案 0 :(得分:0)
我不确定您的数据结构,但考虑到它们是字符串列表:
In [1]: month_report = ['October', 'November']
In [2]: cycle_report = ['October, monthly', 'November, monthly']
In [3]: for month in month_report:
...: for cycle in cycle_report:
...: if month + ', monthly' == cycle:
...: print('yes')
...:
yes
yes