此处是新的Python学习者。我正在尝试制作一个程序,该程序搜索字符串中的单词模式并将其提取为变量。我这样做是通过遍历列表来查找特定的子字符串。
我遇到了一个让我有些困惑的问题,我想知道这里有人可以帮助我吗?
我想在另一个字符串循环中循环浏览一个字符串列表,但是我似乎无法找出在monthcount
变量中循环的位置。我的代码如下:
months = ["Easter '","December"]
monthcount = 0
datecheck = [['dated ',' and inscribed '],['dated ?','verso'],['dated ','lower right'],["dated "+months[monthcount],'in']]
datedcount = 0
while datedcount <(len(datecheck)):
if (datecheck[datedcount][0]) in inscription:
dated = (after(inscription,(datecheck[datedcount][0])))
if dated.isdigit() == False:
dated = (before(dated,(datecheck[datedcount][1])))
dated = dated.strip()
if dated.isdigit() == True:
dated_list[lister] = dated
datedcount = datedcount + 1
答案 0 :(得分:0)
也许这就是您想要的?
datecheck = [['dated ',' and inscribed '],['dated ?','verso'],['dated ','lower right']]
for month in ["Easter '","December"]:
datecheck.append(['dated {0}'.format(month), 'in'])
datedcount = 0
while datedcount < len(datecheck): ...
换句话说,我们用列表中的静态成员初始化datecheck
,然后附加几个动态生成的成员。然后,您可以像以前一样遍历最终列表。
这里没有循环嵌套,只有两个顺序循环,其中第一个循环遍历了我们要添加的表达式。