这是我的尝试,我想要一个3个字母的窗口并走过字符串,但没有得到2的预期答案。我正在进入一些无限循环。想知道为什么。
def find_bob(s1):
check_list = 'bob'
count = 0
n=0
cmp_chars=0
while n<=len(s1):
while cmp_chars == s1[n:3]:
if cmp_chars == check_list:
count += 1
continue
return count
s1= 'azcbobobegghakl'
#check_list='bob'
val1 = find_bob(s1)
答案 0 :(得分:2)
您可以使用files:
"/etc/httpd/conf.d/vhost.conf":
mode: "000644"
owner: root
group: root
encoding: plain
content: |
NameVirtualHost *:80
<VirtualHost *:80>
ServerName staging.example.com
DocumentRoot "/opt/python/current/app"
<Directory "/opt/python/current/app">
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName www.example.com
DocumentRoot "/opt/python/current/app"
<Directory "/opt/python/current/app">
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
使用str.find()
参数来查找bob
,例如:
start
答案 1 :(得分:1)
您可以将regex与zero width lookahead一起使用,并只计算匹配项:
>>> import re
>>> s1= 'azcbobobegghakl'
>>> len([m for m in re.finditer(r'(?=bob)', s1)])
2
答案 2 :(得分:0)
Wondering why...
n=0
cmp_chars=0
while n<=len(s1):
while cmp_chars == s1[n:3]:
if cmp_chars == check_list:
count += 1
continue
n
从零开始,并且在循环内不会改变,因此while条件始终为True。
while cmp_chars == s1[n:3]:
不确定该怎么做,cmp_chars
从零开始,永远不会等于一个字符串,所以这个循环条件总是为假。
尝试
while n<=len(s1):
if s1[n:n+3] == check_list:
count += 1
n = n +1