python 入坑路2
- 列表、元组操作
- 字符串操作
- 字典操作
1.列表、元组操作。
元组不能修改,只能查询。但是元组内嵌套列表,或者字典时,可以修改元祖里面的列表及字典。类似浅拷贝,
a=("a","b",["c","d","e"])
a[2][1]="d1"
print(a)
a=('a', 'b', ['c', 'd1', 'e'])
#删除元祖
del a
#元祖只有 count , index 两个方法
2,列表 增,删,修改、查
1 #列表创建 2 b=["a","b","c",("d","e"),["f","g"]] 3 4 #列表中嵌套的元组 里的元素不能修改,可以修改整个元组 5 6 b[3][0]=1 #执行报错 7 8 b[3]=("d1",“e1”) 可以成功执行。 9 10 #列表增加 11 12 b.append("a") #在列表末尾添加 13 b.insert(1,"b1") #1代表索引,“b1”:要插入的元素,执行效率低 14 b.extend(a) # 把a里所有的元素添加到 列表末尾 15 16 #列表删除 17 del b[1] #1,通用方法 18 b.pop() #默认删除最后一个元素,将删除的元素返回值,括号里可以写索引。 19 b.pop(-2) 20 21 b.remove("a") #填写要删除元素,默认从左开始数 第一个,没有返回值。会导致列表重拍,执行效率低。 22 23 #列表查询 24 b.index("a") #返回索引 25 26 27 28 29
3. 字符串操作
特性:不可修改
1 name.capitalize() 首字母大写 2 name.casefold() 大写全部变小写 3 name.center(50,"-") 输出 '---------------------Alex Li----------------------' 4 name.count('lex') 统计 lex出现次数 5 name.encode() 将字符串编码成bytes格式 6 name.endswith("Li") 判断字符串是否以 Li结尾 7 "Alex\tLi".expandtabs(10) 输出'Alex Li', 将\t转换成多长的空格 8 name.find('A') 查找A,找到返回其索引, 找不到返回-1 9 10 format : 11 >>> msg = "my name is {}, and age is {}" 12 >>> msg.format("alex",22) 13 'my name is alex, and age is 22' 14 >>> msg = "my name is {1}, and age is {0}" 15 >>> msg.format("alex",22) 16 'my name is 22, and age is alex' 17 >>> msg = "my name is {name}, and age is {age}" 18 >>> msg.format(age=22,name="ale") 19 'my name is ale, and age is 22' 20 format_map 21 >>> msg.format_map({'name':'alex','age':22}) 22 'my name is alex, and age is 22' 23 24 25 msg.index('a') 返回a所在字符串的索引 26 '9aA'.isalnum() True 27 28 '9'.isdigit() 是否整数 29 name.isnumeric 30 name.isprintable 31 name.isspace 32 name.istitle 33 name.isupper 34 "|".join(['alex','jack','rain']) 35 'alex|jack|rain' 36 37 38 maketrans 39 >>> intab = "aeiou" #This is the string having actual characters. 40 >>> outtab = "12345" #This is the string having corresponding mapping character 41 >>> trantab = str.maketrans(intab, outtab) 42 >>> 43 >>> str = "this is string example....wow!!!" 44 >>> str.translate(trantab) 45 'th3s 3s str3ng 2x1mpl2....w4w!!!' 46 47 msg.partition('is') 输出 ('my name ', 'is', ' {name}, and age is {age}') 48 49 >>> "alex li, chinese name is lijie".replace("li","LI",1) 50 'alex LI, chinese name is lijie' 51 52 msg.swapcase 大小写互换 53 54 55 >>> msg.zfill(40) 56 '00000my name is {name}, and age is {age}' 57 58 59 60 >>> n4.ljust(40,"-") 61 'Hello 2orld-----------------------------' 62 >>> n4.rjust(40,"-") 63 '-----------------------------Hello 2orld' 64 65 66 >>> b="ddefdsdff_哈哈" 67 >>> b.isidentifier() #检测一段字符串可否被当作标志符,即是否符合变量命名规则 68 True
4.字典
字典一种key - value 的数据类型,
- dict是无序的
- key必须是唯一的,so 天生去重
d={"name":"zhangsan","age":250,"salary":0}
1 #字典创建 2 d={"name":"zhangsan","age":250,"salary":0} 3 4 因为无序,所以没有索引,通过key 进行查找 5 6 if "name" in d.keys(): 7 print(d[name][0])
字典修改,d["name"]=“laowang” ,如果key 存在,就修改值,如果不存在就创建新 key 和value
字典删除 , del d["name"] ,d.pop("name") , d.popitem() 随机删除一个
字典查找,
1 >>> info = {'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya'} 2 >>> 3 >>> "stu1102" in info #标准用法 4 True 5 >>> info.get("stu1102") #获取 6 'LongZe Luola' 7 >>> info["stu1102"] #同上,但是看下面 8 'LongZe Luola' 9 >>> info["stu1105"] #如果一个key不存在,就报错,get不会,不存在只返回None 10 Traceback (most recent call last): 11 File "<stdin>", line 1, in <module> 12 KeyError: 'stu1105'
字典中同理也可以嵌套,列表,字典,元组
1 d1={“name”:("zhangsan","lisi"),"age":[23,25],"salary":{"zhangsan":90000,"lisi":10000}}

浙公网安备 33010602011771号