新网创想网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

python函数易错 python易错点

python内置函数难题

用正则表达式,实现更简单点:

我们提供的服务有:成都网站制作、做网站、外贸营销网站建设、微信公众号开发、网站优化、网站认证、泰来ssl等。为近1000家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的泰来网站制作公司

import re

def str_search(pattern,text):

res=[]

ma=re.finditer(pattern,text)

for i in ma:

res.append(i.start())

return res

pattern='ana'

text='ana ate the banana'

print str_search(pattern,text)

python 内置函数难题

# 1

def str_search(pattern, text):

i_list = []

while True:

try:

i = text.index(pattern)

t = i

if(len(i_list)0):

t = i + 1 + i_list[-1]

i_list.append(t)

text = text[i+1:]

except:break

return i_list

# 2

def str_search(pattern, text):

i_list = []

while True:

try:

i = text.index(pattern)

if(len(i_list)0):

i = text.index(pattern, i_list[-1]+1)

i_list.append(i)

except:break

return i_list

应该没啥问题了,你自己验证下吧。

两种方法,第一种忘记index的可选参数beg了,都可以实现.

# Python index() 方法检测字符串中是否包含子字符串 str ,如果指定 beg(开始) 和 end(结束) 范围,则检查是否包含在指定范围内,该方法与 python find()方法一样,只不过如果str不在 string中会报一个异常。

str.index(str, beg=0 end=len(string))

python定义函数总是报错invalid syntax

不要只关注报错的那行,它的上一行也要检查。

比如上一行如果少了闭合括号 ")",同样也会报错这一行


文章名称:python函数易错 python易错点
网页路径:http://www.wjwzjz.com/article/dosigji.html
在线咨询
服务热线
服务热线:028-86922220
TOP