python的列表、元组、字符串、字典、集合
主要内容:
1.列表、元组操作
2.字符串操作
3.字典操作
4.集合操作
1.列表、元组
列表:
列表是我们最以后最常用的数据类型之一,通过列表可以对数据实现最方便的存储、修改等操作
定义列表
1 1 dog_list = ['小白','小黑','小狗']
通过下标访问列表
1 1 dog_list[1]#正向访问 2 2 dog_list[-1]#反向访问
切片:取多个元素
1 1 dog_list[:2] #如果是从头开始取,0可以忽略,跟上句效果一样 2 2 dog_list[2:] #如果想取最后一个,必须不能写-1,只能这么写 3 3 dog_list[2:-1]#不想取最后一个就写-1 4 4 dog_list[::2] #后面的2是代表,每隔一个元素,就取一个
追加元素
1 1 dog_list.append('小绿')
指定位置插入
1 1 dog_list.insert(2,'小红')
删除元素
1 1 dog_list.remove("小红") #删除指定元素 2 2 dog_list.pop("小红") #删除指定元素,不写则默认删除最后一个 3 3 del dog_list #删除列表
扩展
1 dog_list.extend('旺仔')
拷贝:.copy()
统计:.count()
排序与翻转:.sort()
查询\获取下标:.index()
元组:
元组其实跟列表差不多,也是存一组数,只不是它一旦创建,便不能再修改,所以又叫只读列表
它只有2个方法,一个是count,一个是index。
1 dog_list = ('小白','小黑','小狗')
2.字符串
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
3.字典
字典一种key - value 的数据类型,使用就像我们上学用的字典,通过笔划、字母来查对应页的详细内容。
1 #以三级菜单描述为例 2 data = { 3 '北京':{ 4 "昌平":{ 5 "沙河":["oldboy","test"], 6 "天通苑":["链家地产","我爱我家"] 7 }, 8 "朝阳":{ 9 "望京":["奔驰","陌陌"], 10 "国贸":{"CICC","HP"}, 11 "东直门":{"Advent","飞信"}, 12 }, 13 "海淀":{}, 14 }, 15 '山东':{ 16 "德州":{}, 17 "青岛":{}, 18 "济南":{} 19 }, 20 '广东':{ 21 "东莞":{}, 22 "常熟":{}, 23 "佛山":{}, 24 }, 25 } 26 exit_flag = False 27 28 while not exit_flag: 29 for i in data: 30 print(i) 31 choice = input("选择进入1>>:") 32 if choice in data: 33 while not exit_flag: 34 for i2 in data[choice]: 35 print("\t",i2) 36 choice2 = input("选择进入2>>:") 37 if choice2 in data[choice]: 38 while not exit_flag: 39 for i3 in data[choice][choice2]: 40 print("\t\t", i3) 41 choice3 = input("选择进入3>>:") 42 if choice3 in data[choice][choice2]: 43 for i4 in data[choice][choice2][choice3]: 44 print("\t\t",i4) 45 choice4 = input("最后一层,按b返回>>:") 46 if choice4 == "b": 47 pass 48 elif choice4 == "q": 49 exit_flag = True 50 if choice3 == "b": 51 break 52 elif choice3 == "q": 53 exit_flag = True 54 if choice2 == "b": 55 break 56 elif choice2 == "q": 57 exit_flag = True
4.集合
集合是一个无序的,不重复的数据组合,它的主要作用如下:
- 去重,把一个列表变成集合,就自动去重了
- 关系测试,测试两组数据之前的交集、差集、并集等关系
常用操作
1 s = set([3,5,9,10]) #创建一个数值集合 2 3 t = set("Hello") #创建一个唯一字符的集合 4 5 6 a = t | s # t 和 s的并集 7 8 b = t & s # t 和 s的交集 9 10 c = t – s # 求差集(项在t中,但不在s中) 11 12 d = t ^ s # 对称差集(项在t或s中,但不会同时出现在二者中) 13 14 15 16 基本操作: 17 18 t.add('x') # 添加一项 19 20 s.update([10,37,42]) # 在s中添加多项 21 22 23 24 使用remove()可以删除一项: 25 26 t.remove('H') 27 28 29 len(s) 30 set 的长度 31 32 x in s 33 测试 x 是否是 s 的成员 34 35 x not in s 36 测试 x 是否不是 s 的成员 37 38 s.issubset(t) 39 s <= t 40 测试是否 s 中的每一个元素都在 t 中 41 42 s.issuperset(t) 43 s >= t 44 测试是否 t 中的每一个元素都在 s 中 45 46 s.union(t) 47 s | t 48 返回一个新的 set 包含 s 和 t 中的每一个元素 49 50 s.intersection(t) 51 s & t 52 返回一个新的 set 包含 s 和 t 中的公共元素 53 54 s.difference(t) 55 s - t 56 返回一个新的 set 包含 s 中有但是 t 中没有的元素 57 58 s.symmetric_difference(t) 59 s ^ t 60 返回一个新的 set 包含 s 和 t 中不重复的元素 61 62 s.copy() 63 返回 set “s”的一个浅复制
dog_list.remove("小红") #删除指定元素
dog_list.pop("小红") #删除指定元素
del dog_list #删除指定元素