我的python之路-Day2

时间:2022-10-23 10:03:12

一.列表、元组操作

列表

1.通过下标访问列表中的元素,从0开始

我的python之路-Day2我的python之路-Day2
1 name = ["alex","lol","doat"]
2 >>> names[0]
3 "alex"
4 >>> names[2]
5 "lol"
6 >>> names[-1]
7 "dota"
8 >>> names[-2] #还可以倒着取
9 "lol"
View Code

 2.切片

我的python之路-Day2我的python之路-Day2
1 names = ["alex","zhoujielun","eason","pig"]
2 >>>names[1:2]  #取下标1到2的元素,包括1不包括2
3 ["zhoujielun","eason"]
4 >>>names[:1]  #从头开始取,不需要写0
5 ["alex","zhoujielun"]
6 >>>names[1:] #想取最后一个元素,-1不能写
7 ["zhoujielun","eason","pig"]
8 >>>names[::2] #最后的2表示步长,每隔一个元素,就取一个
9 ["alex","eason"]
View Code

3.追加

我的python之路-Day2我的python之路-Day2
1 >>>names = ['Alex', 'Tenglan', 'Eric', 'Rain', 'Tom', 'Amy']
2 >>>names.append('我是新来的')
3 names =  ['Alex', 'Tenglan', 'Eric', 'Rain', 'Tom', 'Amy','我是新来的']
View Code

4.插入

我的python之路-Day2我的python之路-Day2
1 >>>age = ['11','22','33']
2 >>>age.insert(1,'我是新来的') #在1的元素前面插入
3 
4 age = ['11','我是新来的','22','33']
View Code

5. 修改

我的python之路-Day2我的python之路-Day2
1 >>>names = ["alex","zhouzhou","jiejei"]
2 >>>names[1] = "该换人了"
3 names = ["alex","该换人了","jiejei"]
View Code

6.删除

我的python之路-Day2我的python之路-Day2
1 >>>names = ["alex","zhouzhou","jiejie"]
2 >>>del names[1]
3 names = ["alex","jiejie"]
4 
5 >>>names.pop() #删除列表最后一个元素
6 names = ["alex"]
View Code

7.扩展

我的python之路-Day2我的python之路-Day2
1 >>>names = ["alex","zhouzhou"]
2 >>>b = ["sy"]
3 >>>names.extend(b)
4 names = ["alex","zhouzhou","sy"]
View Code

8.统计

我的python之路-Day2我的python之路-Day2
1 >>>names = ["alex","zhouzhou","juejie"]
2 >>>names.count("alex")
3 0
View Code

9.排序&翻转

我的python之路-Day2我的python之路-Day2
1 >>> names
2 ['Alex', 'Amy', 'Amy', 'Tenglan', 'Tom', '1', '2', '3']
3 >>> names.sort()
4 >>> names
5 ['1', '2', '3', 'Alex', 'Amy', 'Amy', 'Tenglan', 'Tom']
6 
7 >>> names.reverse() #反转
8 >>> names
9 ['Tom', 'Tenglan', 'Amy', 'Amy', 'Alex', '3', '2', '1']
View Code

10.获取下标

我的python之路-Day2我的python之路-Day2
1 >>>names = ["alex","zhouzhou","jiejie"]
2 >>>names.index("jiejie")
3 2
View Code

11.拷贝

我的python之路-Day2我的python之路-Day2
1 >>>names = ["alex","zhouzhou","jiejie"]
2 >>>names2 = names.copy()
3 names2 = ["alex","zhouzhou","jiejie"]
View Code

拷贝真的这么简单吗?

 

我的python之路-Day2我的python之路-Day2
 1 >>>names = ["zhangshaohan","zhoujielun"["alex","jack"],"xiaohong","wangba"]
 2 >>>names2 = names.copy()
 3 >>>print(names,names2)
 4 >>>names[2][0] = "Alex"
 5 >>>print(names)
 6 >>>print(names2)
 7 
 8 ['zhangshaohan', 'zhoujielun', ['alex', 'jack'], 'xiaohong', 'wangba'] ['zhangshaohan', 'zhoujielun', ['alex', 'jack'], 'xiaohong', 'wangba']
 9 ['zhangshaohan', 'zhoujielun', ['Alex', 'jack'], 'xiaohong', 'wangba']
10 ['zhangshaohan', 'zhoujielun', ['Alex', 'jack'], 'xiaohong', 'wangba']
View Code

PS:copy()只copy第一层浅copy,copy第二层的列表等,都不会被copy(只copy了一个内存地址)

 

我的python之路-Day2我的python之路-Day2
 1 >>>import copy
 2 >>>names = ["zhangshaohan","zhoujielun",["alex","jack"],"xiaohong","wangba"]
 3 >>>names2 = copy.deepcopy(names)
 4 >>>print(names)
 5 >>>print(names2)
 6 >>>names[1] = "周杰伦"
 7 >>>names[2][0] = "ALEX"
 8 >>>print(names)
 9 >>>print(names2)
10 
11 ['zhangshaohan', 'zhoujielun', ['alex', 'jack'], 'xiaohong', 'wangba']
12 ['zhangshaohan', 'zhoujielun', ['alex', 'jack'], 'xiaohong', 'wangba']
13 ['zhangshaohan', '周杰伦', ['ALEX', 'jack'], 'xiaohong', 'wangba']
14 ['zhangshaohan', 'zhoujielun', ['alex', 'jack'], 'xiaohong', 'wangba']
View Code

 

元组

元组与列表近似,只是一旦创建,就不能修改,又称只读列表。

所以,它只有两个方法:

  • count
  • index