Shallow Dream

Keep It Simple and Stupid!

0%

第三章_列表简介

不赘述,仅仅记录一些和 C 不同  易混淆  我认为重要的地方

不懂的请使用搜索引擎  看书  询问老师解决  问我(不嫌弃的话

推荐网站:菜鸟教程 https://www.runoob.com/python3/python3-tutorial.html

内容参考:《Python编程 从入门到实践》(第二版)

列表

1
2
3
4
5
subjects = ['chinese','math','english','computer',123,564,'123']
print(subjects) # 可以打印列表
------
Output:
['chinese', 'math', 'english', 'computer', 123, 564, '123']

访问元素

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
subjects = ['chinese','math','english','computer']
print(subjects[0]) # 下标从0开始,范围0 ~ n-1
print(subjects[3])
print(subjects[-1]) # 非常好用,倒数第一个(-几就是倒数第几)
print(subjects[-2]) # 倒数第二个
print(subjects[1+2]) # 当然可以有计算表达式
print(subjects[0].title()) # 当然可以进行修改
------
Output:
chinese
computer
computer
english
computer
Chinese

增、修、删

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
subjects = ['chinese','math','english']
subjects.append('computer') # 添加到列表末尾
print(subjects)
------
Output:
['chinese', 'math', 'english', 'computer']

subjects = ['chinese','math','english']
subjects.insert(0,'computer') # 添加在 0 位置,剩下的元素自动调整
------
Output:
['computer', 'chinese', 'math', 'english']

subjects.insert(1,'computer') # 添加在 1 位置
------
Output:
['chinese', 'computer', 'math', 'english']

subjects.insert(4,'computer') # 添加在大于列表当前元素数的时候
subjects.insert(5,'computer') # 好像是默认添加在末尾
------
Output:
['chinese', 'math', 'english', 'computer']

1
2
3
4
5
6
7
取出需要的元素,修改
subjects = ['chinese','math','english','computer'] # 大学生没有语文课
subjects[0] = 'none'
print(subjects)
------
Output:
['none', 'math', 'english', 'computer']

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
subjects = ['chinese','math','english','computer']
del subjects[0] # 删除取出来的元素,并且不使用,建议del
print(subjects)
------
Output:
['math', 'English', 'computer']

subjects = ['chinese','math','english','computer']
neednot = subjects.pop(0) # .pop(n) 删除(更像是真正意义上的取出,但还是叫删除) n 位置的元素
print(subjects) # 需要使用被删除的元素,建议.pop()
print(neednot)
------
Output:
['math', 'English', 'computer']
Chinese

subjects = ['chinese','math','english','computer'] # 大学生的课太多了,找不到,但我要删除chinese
subjects.remove('chinese') # .remove('你需要删除的元素')
print(subjects)
------
Output:
['math', 'english', 'computer']

subjects = ['chinese','chinese','math','english','computer'] # 大学生有两节语文课
subjects.remove('chinese')
print(subjects)
------
Output:
['chinese', 'math', 'english', 'computer'] # 顺序删除第一个

排序

.sort()

1
2
3
4
5
6
7
8
9
10
11
12
13
str = ['aaab','aaac','aab','aa','a','0','5']
str.sort() # 按规定顺序排序
print(str)
------
Output:
['0', '5', 'a', 'aa', 'aaab', 'aaac', 'aab'] # 排序后就无法恢复到排序前的顺序

str = ['aaab','aaac','aab','aa','a','0','5']
str.sort(reverse = True)
print(str) # 按规定顺序的逆序排序
------
Output:
['aab', 'aaac', 'aaab', 'aa', 'a', '5', '0']

sorted()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
str = ['aaab','aaac','aab','aa','a','0','5']
print(sorted(str)) # 临时排序
print(str)
------
Output:
['0', '5', 'a', 'aa', 'aaab', 'aaac', 'aab'] # 不改变原列表顺序
['aaab', 'aaac', 'aab', 'aa', 'a', '0', '5']

str = ['aaab','aaac','aab','aa','a','0','5']
print(sorted(str,reverse = True)) # 当然可以逆序
print(str)
------
Output:
['aab', 'aaac', 'aaab', 'aa', 'a', '5', '0']
['aaab', 'aaac', 'aab', 'aa', 'a', '0', '5']

reverse()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
str = ['aaab','aaac','aab','aa','a','0','5']
print(str)
str.reverse() # 永久翻转列表排列顺序
print(str)
------
Output:
['aaab', 'aaac', 'aab', 'aa', 'a', '0', '5']
['5', '0', 'a', 'aa', 'aab', 'aaac', 'aaab']

str = ['aaab','aaac','aab','aa','a','0','5']
print(str)
str.reverse() # 当然负负得正嘛!!
print(str)
str.reverse()
print(str)
------
Output:
['aaab', 'aaac', 'aab', 'aa', 'a', '0', '5']
['5', '0', 'a', 'aa', 'aab', 'aaac', 'aaab']
['aaab', 'aaac', 'aab', 'aa', 'a', '0', '5']

len()

1
2
3
4
5
6
nums = ['1','2','3','a',5,9]
lenth = len(nums) # 返回列表长度(元素个数),数个数
print(lenth)
------
Output:
6
end?