1.字符串特性
1)字符串有限序列性:
字符串是一种线性表结构,它的元素个数是有限的。字符串中的每个元素都可以用正负两种索引获取。
2)字符串可比性:
当字符串中的字符来自同一字符集(以ASCII为例)时,可以按照码值进行比较。
比较规则是从左至右,依次比较两个字符串中对应位置上的字符码值。
2.字符串基本操作
1)字符串连接
s = "Hello" + ' ' + "World"
#运行结果:s = "Hello World"
2)字符串复制
s = "ha"*3
#运行结果:s = "hahaha"
3)子串(存在性)判断
print("hel" in "hello")
#运行结果:True
4)字符串切片 s[首:尾:步长] 含头不含尾
s = "千山鸟飞绝,万径人踪灭"
print(s[:-5:-2])
#运行结果:灭人
5)len(s)求字符串长度
s = "Hello World"
print(len(s))
#运行结果:11
6)s.find(y)
#返回字串在字符串中首次出现的位置(数值类型),若找不到返回-1
num = "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)
print(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
2.列表生成式
d1 = [i*i for i in range(10)]
print("d1=",d1)
d2 = [i*i for i in range(10) if i%2==0]
print("d2=",d2)
d3 = [m+n for m in 'ABC' for n in 'XYZ']
print("d3=",d3)
d4 = [s.lower() for s in ["ABC",'EDG','LSP']]
print("d4=",d4)
#运行结果
d1= [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
d2= [0, 4, 16, 36, 64]
d3= ['AX', 'AY', 'AZ', 'BX', 'BY', 'BZ', 'CX', 'CY', 'CZ']
d4= ['abc', 'edg', 'lsp']
3.补充案例:违章判断(书本案例代码优化)
import csv
f = 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()
4.补充案例:行程编码
def readImage(filename=''):
from PIL import Image
im = Image.open(filename)
im = im.convert('1')
#以黑白的方式读取文件,黑0,白255
im_list =''
pix = im.load()
#将图片加载至pix变量
width, height = im.size[0], im.size[1]
#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_list
def Run_Length_Encoding(im_list=''):
new_list = ''; num = 0; e = 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; e = im_list[i]
return new_list
#主程序
import pickle
im_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()
评论 (0)