资源简介 #3.1字符串#字符串特性1.字符串有限序列性字符串是一种线性表结构,它的元素个数是有限的。字符串中的每个元素都可以用正负两种索引获取。2.字符串可比性当字符串中的字符来自同一字符集(以ASCII为例)时,可以按照码值进行比较。比较规则是从左至右,依次比较两个字符串中对应位置上的字符码值。#字符串基本操作#1.字符串连接s = "Hello" + ' ' + "World"#运行结果:s = "Hello World"#2.字符串复制s = "ha"*3#运行结果:s = "hahaha"#3.子串(存在性)判断print("hel" in "hello")#运行结果:True#4.字符串切片 s[首:尾:步长] 含头不含尾s = "千山鸟飞绝,万径人踪灭"print(s[::-1])print(s[:-5:-2])#运行结果:灭踪人径万,绝飞鸟山千 灭人#5.len(s)求字符串长度s = "Hello World"print(len(s))#运行结果:11#6.s.find(y)#返回字串在字符串中首次出现的位置(数值类型),若找不到返回-1num = "hello".find("ell")print(num)#运行结果:1#7.s.split(sep[,num]) #根据sep分割字符串s,num指定切分个数,生成结果为列表类型,sep参数默认为空格s = "1,2,3,4,5,6"s1 = s.split(',')s2 = s.split(',',3)print(s1,s2)#运行结果:['1', '2', '3', '4', '5', '6'] ['1', '2', '3', '4,5,6']#8.s.replace(old,new[,max])#将字符串s中的old用new替换掉,max定义最大替换次数s = "is is is is"s1 = s.replace("is","was")s2 = s.replace("is","was",3)print(s1,s2)#运行结果: was was was was was was was is#9.大小写转换#s.upper() 全部小写转大写 #s.lower() 全部大写转小写s = "abcDEF"s1=s.upper()s2=s.lower()print(s1,s2)#运行结果: ABCDEF abcdef#10.s.count(sub) 统计sub在字符串s中出现的次数s = "is is is is"print(s.count('is ')) #注意空格#运行结果:3#11.iter.join(s) #用iter对s做分隔s = '12345's1 = ','.join(s)print(s1)#运行结果:1,2,3,4,5#12.s.strip(iter) #删除首尾的iter字符,iter默认为空格s = " a b c d e"s1=s.strip()print(s1)x = "_a b c d e"s2=s.strip("_")print(s2)#运行结果:a b c d e a b c d e#字符串补充案例#案例二:违章判断#书本案例代码优化import csvf = open("./shujujiegou/车辆号码.csv",'r') #文件打开方式 r(只读),w(新建),a(追加),+,b(二进制)f_csv = csv.reader(f) #读取CSV文件内容,结果为可迭代对象f_csv_list = list(f_csv)print(f_csv_list[0][0],' ',f_csv_list[0][1])for row in f_csv_list[1::]: #逐行枚举车辆信息 pos = False #默认为外地车 if "A"<=row[0][1]<="Z" and row[0][1] == "B": #检查车牌的归属地 pos = True #确认为本地车 if pos == False and 6!=w!=7: print(row[0],' ',row[1]) if pos == True: car_list = list(row[0])[::-1] #将车牌转为list后逆序 for i in car_list: if '0'<=i<='9': num = int(i) break num = False if num == False: #车牌内没有数字 continue elif num == 0 or num == 5: t = 5 else: t = min(num,10-num) if t == int(row[3]): print(row[0],' ',row[1])f.close()#查找电话号码#案例三:通过正则表达式实现import rephone = pile(r'\d\d\d\d-\d\d\d\d\d\d\d\d')x = input("请输入一个字符串:")mo = phone.search(x) #查找x中第一个符合phone的格式的匹配if mo: print("电话号码为:",mo.group())else: print("没有找到电话号码")扩展:1.匹配特定字符例:[Pp]ython 匹配结果 Python,python2.匹配区间例1:[a-z]hs 匹配结果 ahs,bhs...zhs例2:[a-zA-Z0-9]hs 匹配结果 ahs,Fhs,8hs3.快捷键 %d=[0-9] %w=[a-zA-Z0-9] %D=[^0-9] %W=[^a-zA-Z0-9]例1:%d%d 匹配结果 23,76例2:%w%d 匹配结果 a5,f4,684.扩展 {m} {m,n}例1:abc{2} 匹配结果 abcc例2:abc{2,5} 匹配结果 abcc...abccccc例3:abc{2,} 匹配结果 abcccc(至少两个)5.扩展快捷键 *={0,} +={1,} ={0,1}例1:ab*c 匹配结果 ac,abbbc,abbbbbbbc例2:ab+c 匹配结果 abc,abbbc例3:ab c 匹配结果 ac,abc#案例四:行程编码def readImage(filename=''): from PIL import Image im = Image.open(filename) im = im.convert('1') # 以黑白的方式读取文件,黑0,白255 im_list ='' pix = im.load() # 将图片加载至pix变量 width = im.size[0] # size中有两个参数,第1个为图像宽度值 height = im.size[1] # 第2个参数为图像高度值 #im.show(filename) for x in range(width): #s = '' for y in range(height): im_list += str(pix[x,y]//255) # 根据像素点坐标获得该点的 RGB 值 #s += str(pix[x,y]//255) #print(s) #用于逐行输出,使结果更加直观 return im_listdef Run_Length_Encoding(im_list=''): new_list = '' num = 0 pre = im_list[0] for i in range(len(im_list)): if im_list[i] == pre: num += 1 if i == len(im_list) - 1: new_list += str(num)+ ' ' + pre elif i != len(im_list) - 1: new_list += str(num)+ ' ' + pre + ' ' num = 1 pre = im_list[i] return new_list#主程序import pickleim_list1 = readImage('./shujujiegou/岩.bmp') #读取图像并转为0,1列表f=open('./shujujiegou/图像编码.txt','wb')pickle.dump(im_list1, f) # 将列表对象转换为二进制文件f.close()im_list2=Run_Length_Encoding(im_list1) #利用行程编码压缩内容print(im_list2)f=open("./shujujiegou/行程编码.txt","wb")pickle.dump(im_list2,f)f.close() 展开更多...... 收起↑ 资源预览