python 列表元组字符串字典集合return等梳理

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 列表元组字符串字典集合return等梳理相关的知识,希望对你有一定的参考价值。

 

 

有必要对这些数据类型及操作做下梳理:

1.列表:增删改查  

a.查找:

技术分享
 1 >>> names=["zhang","wang","li","zhao"]
 2 #列表天生具有下标,基于下标0,1,2,...进行查找
 3 >>> names[1] 
 4 wang
 5 #列表的切片,即也是基于下标进行操作
 6 >>> names[1:3]
 7 [wang, li]
 8 >>> names[-1:]
 9 [zhao]
10 >>> names[:3]
11 [zhang, wang, li]
12 >>> names[1:]
13 [wang, li, zhao]
14 #基于列表中的值找下标
15 >>> print(names.index(li))
16 2
17 >>> print(names[names.index(li)])
18 li
19 #基于for循环查找列表值
20 >>> for i in names:
21 ...     print(i)
22 ...
23 zhang
24 wang
25 li
26 zhao
View Code

b.修改

技术分享
 1 >>> names=["zhang","wang","li","zhao"]
 2 >>> names.append("Sun")
 3 >>> names
 4 [zhang, wang, li, zhao, Sun]
 5 >>> names.insert(1,qian)
 6 >>> names
 7 [zhang, qian, wang, li, zhao, Sun]
 8 >>> names[2] = gao
 9 >>> names
10 [zhang, qian, gao, li, zhao, Sun]
View Code

c.删除

技术分享
 1 >>> names
 2 [zhang, qian, gao, li, zhao, Sun]
 3 >>> names.remove(zhang)
 4 >>> names
 5 [qian, gao, li, zhao, Sun]
 6 >>> del names[1]
 7 >>> names
 8 [qian, li, zhao, Sun]
 9 >>> names.pop()
10 Sun
11 >>> names.pop(1)
12 li
13 >>> names
14 [qian, zhao]
View Code

d.增加

技术分享
 1 >>> names
 2 [qian, zhao]
 3 >>> names.append(Sun) #追加
 4 >>> names
 5 [qian, zhao, Sun]
 6 >>> names.insert(1,zhang)  #插入
 7 >>> names
 8 [qian, zhang, zhao, Sun]
 9 >>> names2=[1,2,3,4]
10 >>> names.extend(names2) #合并
11 >>> names
12 [qian, zhang, zhao, Sun, 1, 2, 3, 4]
13 #深浅copy
14 >>> names3 = names.copy()#浅copy,指向同一块内存地址
15 >>> names
16 [qian, zhang, zhao, Sun, 1, 2, 3, 4]
17 >>> names3
18 [qian, zhang, zhao, Sun, 1, 2, 3, 4]
19 #深copy,新增一块内存地址,把数据完全copy一份到新内存
20 >>> import copy
21 >>> names
22 [qian, zhang, zhao, Sun, 1, 2, 3, 4]
23 >>> names4 = copy.deepcopy(names)
24 >>> names4
25 [qian, zhang, zhao, Sun, 1, 2, 3, 4]
View Code

2.元组:只能查询和切片,也叫只读列表,只有count和index方法

技术分享
 1 >>> names = (alex,wang,eric)
 2 >>> names.append(li)
 3 Traceback (most recent call last):
 4   File "<stdin>", line 1, in <module>
 5 AttributeError: tuple object has no attribute append
 6 >>> names[1]
 7 wang
 8 
 9 >>> print(names.index(alex))
10 0
11 >>> print(names[names.index(alex)])
12 alex
View Code

3.字符串:文件中只能存储字符串,且不可修改

技术分享
 1 name="my name is alex"
 2 print(name.center(50,"-")) 打印50个字符,不够用-补上,并把name放中间
 3 -----------------my name is alex------------------
 4 >>> print(name.startswith(my)) #判断是否以某字符开头
 5 True
 6 print(name.endswith("ex")) 判断name字符串是否以ex结尾
 7 My name is alex
 8 
 9 name="my\\tnameis{name}andmyageis{year}old"
10 print(name.format(name=alex,year=23))
11 my name is alex and my age is 23 old
12 print(name.index(a))
13 5
14 print(123.isdigit())是否为整数
15 True
16 print(+.join([1,2,3,4]))  经常用
17 1+2+3+4
18 print(alexli.replace(l,L)) 把小写l替换成大写的L
19 aLex Li
20 print(alex li.split())把字符串按空格组成一个列表
21 [alex, li]
View Code

4.字典:key:value的数据类型,无序

>>> info = {‘stu1101‘:‘wang‘,‘stu1102‘:‘zhang‘,‘stu1103‘:‘li‘}

a.增加

技术分享
 1 >>> info = {stu1101:wang,stu1102:zhang,stu1103:li}
 2 >>> info[stu1104] = zhao
 3 >>> info
 4 {stu1101: wang, stu1102: zhang, stu1103: li, stu1104: zhao}
 5 
 6 #合并
 7 >>> info2 = {1:3,2:5}
 8 >>> info.update(info2)
 9 >>> info
10 {stu1101: wang, stu1102: zhang, stu1103: li, stu1104: zhao, 1:
11 , 2: 5}
View Code

b.修改

技术分享
1 >>> info = {stu1101:wang,stu1102:zhang,stu1103:li}
2 >>> info[stu1101] = lu
3 >>> info
4 {stu1101: lu, stu1102: zhang, stu1103: li}
View Code

c.删除

技术分享
1 >>> info
2 {stu1101: lu, stu1102: zhang, stu1103: li}
3 >>> del info[stu1101]
4 >>> info
5 {stu1102: zhang, stu1103: li}
6 
7 info.pop("stu1101")  #pop删除
8 info.popitem()随机删除
View Code

d.查询

技术分享
 1 >>> info
 2 {stu1102: zhang, stu1103: li}
 3 >>> info[stu1102]
 4 zhang
 5 #判断是否存在字典中
 6 print(info.get(stu1104))  安全获取的方法
 7 None
 8 >>> print(stu1103 in info)
 9 True
10 
11 >>> info
12 {stu1102: zhang, stu1103: li}
13 >>> info[stu1102]
14 zhang
15 >>> for i in info:  #最建议的查询方式
16 ...     print(i,info[i])
17 ...
18 stu1102 zhang
19 stu1103 li
20 >>> for k,v in info.items():
21 ...     print(k,v)
22 ...
23 stu1102 zhang
24 stu1103 li
25 
26 >>> print(info.items())
27 dict_items([(stu1102, zhang), (stu1103, li)])
View Code

e.多级字典的查询方式

技术分享

5.集合:无序,天生去重

技术分享
1 >>> list = [1,2,3,4]
2 >>> list_1 = set(list)
3 >>> print(type(list_1),list_1)
4 <class set> {1, 2, 3, 4}
View Code

关系型测试:

交差并子父对称集操作

a.增加

技术分享
 1 >>> list = [1,2,3,4]
 2 >>> list_1 = set(list)
 3 >>> print(type(list_1),list_1)
 4 <class set> {1, 2, 3, 4}
 5 >>> list_1.add(999)
 6 >>> list_1
 7 {1, 2, 3, 4, 999}
 8 >>> list_1.update([777,888])
 9 >>> list_1
10 {1, 2, 3, 4, 999, 777, 888}
View Code

b.删除

技术分享
1 >>> list_1.remove(999)
2 >>> list_1
3 {1, 2, 3, 4, 777, 888}
4 >>> list_1.discard(888)
5 >>> list_1
6 {1, 2, 3, 4, 777}
7 >>> list_1.pop()
8 1
View Code

c.深浅复制,跟列表一样

d.判断

len(list_1)  #测试长度

判断是否存在集合中,大于小于等

6.return

两个作用:

1.得到程序的执行结果;

2.程序结束,return后的代码不再执行;

 

技术分享

以上是关于python 列表元组字符串字典集合return等梳理的主要内容,如果未能解决你的问题,请参考以下文章

对比Python中的列表元组字典集合字符串等之间异同

python数据类型——列表元组字典集合

Python3 基本数据类型:字符串列表元组集合字典等

Python基础数据类型(数字字符串布尔列表元组字典集合等)

python基础之 列表元组操作 字符串操作 字典操作 集合操作 文件操作 字符编码与转码

Python 02--列表字典集合