find函数python(正则表达式find函数)

admin 514 0

大家好,find函数python相信很多的网友都不是很明白,包括正则表达式find函数也是一样,不过没有关系,接下来就来为大家分享关于find函数python和正则表达式find函数的一些知识点,大家可以关注收藏,免得下次来找不到哦,下面我们开始吧!

一、python 如何根据输入参数调用不同的函数

1、func_dict={"a":funcA,"b":funcB}

2、returnfunc_dict.get(x,func_None)()

在有switch的语言中,一般都是使用switch来根据入参进行判断。但是python中没有switch,因为根本不需要!!使用字典代替switch,性能更高,而且这种方法的表述能力更强一点。

3、在有switch的语言中,一般都是使用switch来根据入参进行判断。但是python中没有switch,因为根本不需要!!使用字典代替switch,性能更高,而且这种方法的表述能力更强一点。

4、另外func_dict.get(x, func_None)()中方法是从字典中取出值对应的函数对象,然后后面加上()是执行该对象的__call__方法。因为python中函数就是实现了__call__方法的对象。所以可以这么使用。

二、find在python中的用法

1、Python的find函数多用在字符串的处理上,也是Python计算机二级的小考点。

2、定义:Python find()方法检测字符串中是否包含子字符串 str,如果指定 beg(开始)和 end(结束)范围,则检查是否包含在指定范围内,如果包含子字符串返回开始的索引值,否则返回-1。

3、str.find(str, beg=0, end=len(string))

4、end---结束检索,默认为字符串的长度

5、如果包含子字符串返回开始的索引值,否则返回-1。

6、二、find函数的详细函数使用解释

7、第一个例子(tips:可以复制代码到idle里边,然后自己运行着看,这个看不懂可以再看看第二个例子,结合着理解)

8、print(str.find("a")) #从下标0开始,查找在字符串里第一个出现的子串,返回结果

9、print(str.find("a",1))#从下标1开始,查找在字符串里第一个出现的子串:返回结果6

10、print(str.find("7"))#查找不到返回-1

11、第二个例子(tips:可以把代码沾到idle中,运行然后对照着我的下边的解答看)

12、str="hello Best wishes to you";

13、第一个从字符h(下标为0)开始数起,一直到检索到t停止,一直检索过来,是18个字符串

14、第二个从第10个字符串开始数起,只要后面有to这个字符,那么就返回开始的索引值,也就是18

15、第三个从第18个字符串开始数起,刚好落在t字符串上,从t开始检索,那往后开始检索肯定没问题,继续返回18

16、第四个从第19个字符串开始数起,也就是to的o后面的空格,从空格往后检索,那么就没有想要的内容了,所以返回-1

17、第五个print里边我添加了end的参数,因为直接定位在19,所以相当于o检索不上,所以输出-1

18、第六个print我更改end为20,这时候就可以正常输出了

19、第七个print里边我把17改为18,19改为20,依然可以输出,理由同上,举6反1。

三、Python 的题

from selenium import webdriver#用来驱动浏览器的from selenium.webdriver import ActionChains#破解滑动验证码的时候用的可以拖动图片import timefrom PIL import Image# pip3 install pillowimport random#截图图片函数def cut_image(driver):#获取整个页面图片,图片名字为'snap.png'

driver.save_screenshot('snap.png')#获取滑动小画图

image= driver.find_element_by_class_name('geetest_canvas_img') print(image.location) print(image.size)#获取小图片的左上右下的位置

left= image.location['x']

top= image.location['y']

right= left+ image.size['width']

buttom= top+ image.size['height'] print(left, top, right, buttom)#调用open方法打开全屏图片并赋值给image_obj对象

image_obj= Image.open('snap.png')#通过image_obj对象对小图片进行截取

# box: The crop rectangle, as a(left, upper, right, lower)-tuple.

img= image_obj.crop((left, top, right, buttom))#打开截取后的小图片

return img#获取完整图片def get_image1(driver):

time.sleep(2)#修改document文档树,把完整图片的display属性修改为block

var x= document.getElementsByClassName("geetest_canvas_fullbg")[0].style.display="block";'''

#执行js代码 driver.execute_script(js_code)#截取图片

image= cut_image(driver) return image#获取有缺口图片def get_image2(driver):

time.sleep(2)#修改document文档树,把完整图片的display属性修改为block

var x= document.getElementsByClassName("geetest_canvas_fullbg")[0].style.display="none";'''

#执行js代码 driver.execute_script(js_code)#截取图片

image= cut_image(driver) return image#获取滑块滑动距离def get_distance(image1, image2):#小滑块右侧位置

num= 60 print(image1.size) for x in range(start, image1.size[0]): for y in range(image1.size[1]):#获取image1完整图片每一个坐标的像素点

rgb1= image1.load()[x, y]#获取image2缺口图片每一个坐标的像素点

rgb2= image2.load()[x, y]#(60, 86, 40)(60, 86, 40) rgb

print(rgb1, rgb2)# abs获取绝对值,像素点比较的值

b= abs(rgb1[2]- rgb2[2])#如果条件成立,则找到缺口位置

if not(r< num and g< num and b< num):#有误差- 7像素

return x- 7#模拟人的滑动轨迹def get_strck_move(distance):

s= v0* t+ 0.5* a*(t** 2)'''

move_list= []#中间值,作为加减速度的位置

mid= distance/ 5* 3#加减速度列表

while s< distance: if s< mid:#随机获取一个加速度

a= v_list[random.randint(0, len(v_list)- 1)] else:#随机获取一个减速度

a=-v_list[random.randint(0, len(v_list)- 1)]'''

s= v* t+ 0.5* a*(t**2)'''

m_v= v+ a* t#把当前加/减速度赋值给初始速度,以便下一次计算

v0= m_v#把位移添加到滑动列表中 move_list.append(s1)#修改滑动初始距离

s+= s1#后退列表,自定义后退滑动轨迹,必须是负值

back_list= [-1,-1,-2,-3,-2,-1,-1,-2,-3,-2,-1,-1] return{'move_list': move_list,'back_list': back_list}def main():

driver= webdriver.Chrome(r'F:\python学习\Scripts\chromedriver')

driver.implicitly_wait(10) try:

driver.get('https://account.cnblogs.com/signin?returnUrl=https%3A%2F%2Fwww.cnblogs.com%2F')# 1、输入用户名与密码,并点击登录

user_input= driver.find_element_by_id('LoginName')

user_input.send_keys('你的博客园账号')

pwd_input= driver.find_element_by_id('Password')

pwd_input.send_keys('你的博客园密码')

login_submit= driver.find_element_by_id('submitBtn')

login_submit.click()# 2、获取完整的图片

image1= get_image1(driver)# 3、获取有缺口图片

image2= get_image2(driver)# 4、比对两张图片,获取滑动距离

distance= get_distance(image1, image2) print(distance)# 5、模拟人的滑动轨迹

move_dict= get_strck_move(distance)#获取前进滑动轨迹

move_list= move_dict['move_list']#获取后退滑动轨迹

back_list= move_dict['back_list']# 6、开始滑动

move_tag= driver.find_element_by_class_name('geetest_slider_button')#点击摁住滑动按钮 ActionChains(driver).click_and_hold(move_tag).perform()#向前滑动

ActionChains(driver).move_by_offset(xoffset=move, yoffset=0).perform()

ActionChains(driver).move_by_offset(xoffset=back, yoffset=0).perform()

ActionChains(driver).move_by_offset(xoffset=3, yoffset=0).perform()

ActionChains(driver).move_by_offset(xoffset=-3, yoffset=0).perform()

time.sleep(0.1)#释放滑动按钮 ActionChains(driver).release().perform()

driver.close()if __name__=='__main__':

四、python的findall函数调用总是出错,请教要怎么解决

1、我一般都是用math或者seach的,你可以试试。

2、re.match尝试从字符串的开始匹配一个模式,如:下面的例子匹配第一个单词。

3、import retext="JGood is a handsome boy, he is cool, clever, and so on..."

4、m= re.match(r"(\w+)\s", text)

5、print m.group(0),'\n', m.group(1)

6、print'not match're.match的函数原型为:re.match(pattern, string, flags)第一个参数是正则表达式,这里为"(\w+)\s",如果匹配成功,则返回一个Match,否则返回一个None;第二个参数表示要匹配的字符串;第三个参数是标致位,用于控制正则表达式的匹配方式,如:是否区分大小写,多行匹配等等。re.searchre.search函数会在字符串内查找模式匹配,只到找到第一个匹配然后返回,如果字符串没有匹配,则返回None。import re

7、text="JGood is a handsome boy, he is cool, clever, and so on..."

8、m= re.search(r'\shan(ds)ome\s', text)

9、print'not search're.search的函数原型为: re.search(pattern, string, flags)每个参数的含意与re.match一样。 re.match与re.search的区别:re.match只匹配字符串的开始,如果字符串开始不符合正则表达式,则匹配失败,函数返回None;而re.search匹配整个字符串,直到找到一个匹配。

关于find函数python到此分享完毕,希望能帮助到您。