join
# s=‘alexwusir‘ # print(‘*‘.join(s)) # 输出:a*l*e*x*w*u*s*i*r
计算用户输入的有多少整数?isdigit:判断字符串只由数字组成
content=input(‘输入:‘) count=0 for i in content: if i.isdigit(): count+=1 print(count)
append 增加
l1=[‘123‘,‘aas‘,456,‘qwe‘] while True: a=input(‘/Q或q退出‘) if a.upper()==‘Q‘:break l1.append(a) print(l1) print(l1)
insert:插入
l1=[‘123‘,‘aas‘,456,‘qwe‘] l1.insert(2,‘kwe‘) print(l1) 输出:[‘123‘, ‘aas‘, ‘kwe‘, 456, ‘qwe‘] l1=[‘123‘,‘aas‘,456,‘qwe‘] l1.insert(0,123) print(l1) 输出:[123, ‘123‘, ‘aas‘, 456, ‘qwe‘] l1=[‘123‘,‘aas‘,456,‘qwe‘] l1.extend(‘qwert‘) print(l1) 输出:[‘123‘, ‘aas‘, 456, ‘qwe‘, ‘q‘, ‘w‘, ‘e‘, ‘r‘, ‘t‘]
extend:
l1=[‘123‘,‘aas‘,456,‘qwe‘] l1.extend([1,2,44,5]) print(l1) 输出:[‘123‘, ‘aas‘, 456, ‘qwe‘, 1, 2, 44, 5]
pop:删除。有返回值。
l1=[‘123‘,‘aas‘,456,‘qwe‘] l1.pop() print(l1) 输出:[‘123‘, ‘aas‘, 456] l1.pop(0) print(l1) 输出:[‘aas‘, 456, ‘qwe‘] l1=[‘123‘,‘aas‘,456,‘qwe‘] a=l1.pop(2) print(a) 输出:456 (有返回值) print(l1) 输出:[‘123‘, ‘aas‘, ‘qwe‘]
remove:按照元素去删除。
l1=[‘123‘,‘aas‘,456,‘qwe‘] l1.remove(‘123‘) print(l1) 输出:[‘aas‘, 456, ‘qwe‘] l1.remove(456) print(l1) 输出:[‘123‘, ‘aas‘, ‘qwe‘]
clear 清空列表
l1=[‘123‘,‘aas‘,456,‘qwe‘] l1.clear() print(l1) 输出:[]
del:功能1,删除列表。
l1=[‘123‘,‘aas‘,456,‘qwe‘] del l1 print(l1) del:功能2,可以按照切片,按照索引去删除 l1=[‘123‘,‘aas‘,456,‘qwe‘] del l1[0:3] print(l1) 输出:[‘qwe‘]
改,按索引改
l1=[‘123‘,‘aas‘,456,‘qwe‘] l1[0]=345 print(l1) 输出:[345, ‘aas‘, 456, ‘qwe‘] 按切片改 l1=[‘123‘,‘aas‘,456,‘qwe‘] l1[0:2]=[‘sb‘,567] print(l1) 输出:[‘sb‘, 567, 456, ‘qwe‘]
查 按切片,按索引查
l1=[‘123‘,‘aas‘,456,‘qwe‘] print(l1[:3]) 输出:[‘123‘, ‘aas‘, 456] for循环查 l1=[‘123‘,‘aas‘,456,‘qwe‘] for i in l1: print(i) 输出:123 aas 456 qwe
len,长度。count,出现次数。
l1=[‘123‘,‘aas‘,456,‘qwe‘] print(len(l1)) 输出:4 l1=[‘123‘,‘aas‘,456,‘qwe‘] ret=l1.count(456) print(ret) 输出:1
index:通过元素找索引
l1=[‘123‘,‘aas‘,456,‘qwe‘] print(l1.index(456)) 输出:2
sort 从小到大
l2=[1,3,4,2,6,5,8] l2.sort() print(l2) 输出:[1, 2, 3, 4, 5, 6, 8] sort(reverse) 从大到小 l2=[1,3,4,2,6,5,8] l2.sort(reverse=True) print(l2) 输出:[8, 6, 5, 4, 3, 2, 1]
reverse() 倒叙
l2=[1,3,4,2,6,5,8] l2.reverse() print(l2) 输出:[8, 5, 6, 2, 4, 3, 1]
首字母大写
l1=[‘小新‘,‘alex‘,‘wusir‘,[1,‘taibai‘,‘ritian‘,2]] l1[1]=l1[1].capitalize() print(l1[1]) 输出:Alex
大写
l1=[‘小新‘,‘alex‘,‘wusir‘,[1,‘taibai‘,‘ritian‘,2]] l=l1[3] s=(l[1].upper()) l1[3][1]=s print(s) 输出:TAIBAI
循环打印每一个元素,遇到列表再打印里面的
l1 = [1,2,3,‘alex‘,[2,3,‘wusir‘],7] for i in l1: if type(i)==list: for j in i: print(j) else:print(i)
li = [1,3,4,‘alex‘,[3,7,8,‘taibai‘],5,‘ritian‘] for i in range(0,len(li)): if type(li[i]) == list: for j in li[i]: print(j) else: print(li[i])
l1 = [1,2,3,‘alex‘,[2,3,‘wusir‘],7,4,5,8,9,1] for i in range(0,len(l1)): if type (l1[i])==list: for a in l1[i]: print(a) else: print(l1[i])
range的用法:指定范围,生成指定数字。范围列表[],列表中的元素是数字,且可控的有效数字范围,一般与for 配合使用
or i in range(1,5): print(i) #运行结果:1234 for i in range(4): print(i) #运行结果:0123 for i in range(1,10,2): print(i) #运行结果:13579 for i in range(5,0,-1): print(i) #运行结果:54321 for i in range(5,-2,-1): print(i) #运行结果:543210-
原文地址:https://www.cnblogs.com/qwertyu/p/8352192.html
时间: 2024-11-04 14:56:11