python基础:集合,文件操作,字符串方法等
切片:
# li = [1,2,3,4,5]li = list(range(1,11)) # range 生成器print(li)print(li[1:3:1])# 开始 结束 步长print(li[1:11:2])print(li[-1:-5:-1])print(li[::-1])# 输入: "A man, a plan, a canal: Panama"# 输出: true#a = "A man, a plan, a canal: Panama"#a= a.replace(",","").replace(":",'').replace(" ",'').lower()print(a)
字符串方法:
a = " abc11defghijklmn\n.jpg "print(a.index("b",0,9)) #找不到报错print(a.find("b")) #找不到返回-1print(a.strip('.jpg')) #去2边空格和换行符,传值去指定的字符串print(a.lstrip()) #去左边的空格print(a.rstrip()) #去右边的空格print(a.lower()) #小写print(a.upper()) #大写print(a.count('a')) #统计字符串出现的次数print(a.isdigit()) #判断字符串是否为整数print(a.isalnum())#字符串不包含特殊符号,返回trueprint(a.isalpha()) #如果是字母或汉字,返回true# 字符串格式化 %s f {b}b = 11233# print(b.format())# print(b.format_map())print(f"zifuchuan:{b}") b.startswith() #判断字符串以”“开始b.endswith() #判断字符串以”“结尾b.zfill(6) #补0的# 字符串替换,1只替换一次的b.replace("旧的字符串","新的字符串").replace("空格",'',1) b.title()#每个首字母大写b.capitalize() #首字母大写b.istitle() #判断首字母是不是都大写b.isspace()#判断是否空格b.center(50,'*') #长度50,不够*补,字符串放中间b.isupper() #判断是否都为大写字母b.islower() #判断是否都为小写字母c = '12,3,345,567'print (c.split(',')) #分割字符串,分割后会返回list# c.split() #分割字符串,没有没有指定字符串,就按空字符串分割d = ['q','e','r','y']# 连接字符串d1 = '=='.join(d)print(d1) g = '123'g1 = '=='.join(g)print(g1)
import stringprint(string.digits)#所有整数print(string.ascii_lowercase)#所有小写字母print(string.ascii_uppercase)#所有大写字母print(string.ascii_letters)#所有大写字母/小写字母print(string.punctuation)#所有符号print(list(string.digits))#转listprint(set(string.digits))#转集合
回文算法:
s = input("s:").strip() s_list = []for i in s: if i.isalnum(): s_list.append(i) new_s = ''.join(s_list).lower()if new_s == new_s[::-1]: print("回文")else: print("不是")
三元表达式,列表生成式,变量交换:
# 三元表达式a = 17a2 = ''if a >=18: a2 = "chengnian"else: a2 = "weichengnian"a3 = "chengnian" if a >=18 else "weichengnian"print(a2)print(a3)#列表生成式l = list(range(1,11)) l2 = []for i in l: if i %2 ==0: l2.append(i) l3 = [ i for i in l if i%2==0 ]# l4 = [str(i) for i in l]print(l2)print(l3)#变量交换a = 1b = 2#第一种# temp = None# temp = b# b = a# a = temp# print("a=%s"%a,"b=%s"%b)#第二种a,b = b,aprint(f'a={a},b={b}')#第三种 int# a = a + b #3# b = a - b #1# a = a - b #2# print(f'a={a},b={b}')
浅拷贝与深拷贝:
import copy l = [1,1,2,3,4,5,6,7,8,[1,2,3]]#浅拷贝#l2 = l #浅拷贝,内存地址不变# l2 = l.copy() #浅拷贝,更换内存地址# l2 = l.copy.copy(l)#浅拷贝#深拷贝l2 = copy.deepcopy(l)#内存地址print(id(l))print(id(l2)) l2[-1][1] = 'ljq'print(l)print(l2)# 正在循环的list,删除里面的元素会导致下标错乱# l2 = [1,1,2,3,4,5,6,7,8]# for i in l2:# if i % 2 != 0:# l.remove(i)# # print(l)
集合操作:
# 集合天生去重,集合是无序的,没有下标# s1 = set() #空集合# s = {1,2,3,4,5,5}# print(s)# l = [1,2,3,4,5,6,777,7,7,7,7,7,7,7,6]# print(list(set(l)))# l = [1,1,2,2,3,4,4,5,5,6,6,7,7]# for i in set(l): # l= set(l) 减少循环的次数# if l.count(i) ==1:# print(i)# break# s = {1,2,3,4,5,5}# print(s)# s.add(6) #增加# print(s)# s.update({7,8,9}) #增加# print(s)# s.pop()#删除第一个# print(s)s1 = {1,2,3,4,5} s2 = {3,4,5,6,7} s3 = {5,8}#交集print(s1.intersection(s2).intersection(s3))print(s1 & s2 & s3)#并集print(s1.union(s2).union(s3))print(s1 | s2 | s3)#差集,在前面集合有,后面的集合没有的print(s1.difference(s2))print(s1 - s2 - s3)#对称差集,交集之外的print(s1.symmetric_difference(s2))print(s1 ^ s2 ^ s3)
文件操作:
# #打开文件不存在会报错,中文加 encoding='utf-8'#写# n = ["1","2",'3']n1 = [1,2,3,4,5] f = open("a.txt",'w',encoding='utf-8')# for i in n1:# i = str(i)+'\n'# f.write(i)#只能写字符串# f.writelines(n)n2 = [str(i)+'\n' for i in n1] f.writelines(n2)# # result = f.read()f.close()#读# f = open("a.txt",'r',encoding='utf-8')# result = f.read()# f.close()# print(result)f = open("a.txt",encoding='utf-8')# f.write("ni好!")# result = f.read()#读取所有,返回字符串# result = f.readlines()#读取所有,返回list# print(f.readline())#读取1行# print(f.readline())# print("f.readline",f.readline())# print("f.read",f.read())# f.seek(0)#把文件指针移到0开头的位置# print("f.readlines",f.readlines())# f.close()# print(result)#文件打开模式 9种 读 写 追加模式# r w a# r 只能读,不能写,文件不存在会报错# w 只能写,不能读,文件不存在会创建,文件已存在会清空里面的内容# a 只能写不能读,文件不存在会创建,文件已存在会末尾追加# f = open("a.txt",'a',encoding='utf-8')# f.write("ni好!")# # result = f.read()# f.close()# print()###########错误写法# r模式写 报错not writable# w模式读会报错 not readable# w模式再写会覆盖# f = open("a.txt",'r',encoding='utf-8')# f.write("ni好!")# # result = f.read()# f.close()# f = open("a.txt",'w',encoding='utf-8')# # f.write("ni好!")# result = f.read()# f.close()# print(result)