list 列表(属于collection中的一种)
list类型,有序可重复,可变
list内的数据可以混合,string + int等
定义空列表:
1)变量名 = []
或
2)变量名 = list()
一、取出集合内元素:
list = ["hello", 11, 33, "world"] (index索引从0开始)
正索引:0 1 2 3
逆索引:-4 -3 -2 -1
单个取出:(变量接收) = list[0]
批量取出:(变量接收) = list[0:2] (此处范围包左不包右,取出的元素索引为0和1)
list = [0, 1, 2, 3, 4, 5, 6, 7]
批量取出并设置跨度:(变量接收) = list[0:4:2] #取索引0-3,跨度为2,结果[0, 2]
全部取出并设置跨度2,写法为:(变量接收) = list[::2] # [0, 2, 4, 6]
也可以负跨度:(变量接收) = list[4::-1] # [4, 3, 2, 1, 0]
反向取出:
(变量接收) = list[-1] (-1索引为最右边字符)
即:list[2] 与 list[-2] 结果相同
二、列表推导式:
要求:添加1-50的每个数字到list集合中
原写法:
list = []
for i in range(1, 51):
list.append(i)
print(list)
现写法:
list = [i for i in range(1, 51)] 先运行循环(1),后添加(2)
(2) (1)
print(list)
可以使用 list = [i*2 for i in range(1, 51)]
也可以其他操作
要求:将首都的首字母大写
cities = ['madrid', 'paris', 'lisabon']
list = [i.capitalize() for i in cities]
print(list) # ['Madrid', 'Paris', 'Lisabon']
还可以更复杂处理,加判断条件,再大写首字母
cities = ['madrid', 'paris', 'lisabon']
list = [i.capitalize() for i in cities if i[0] == "m"]
print(list) # ['Madrid']
整理出新集合,每个元素含有“ball”关键词
sports = ["Football", "Basketball", "Tennis", "Golf", "Volleyball"]
list = [x for x in sports if "ball" in x]
print(list)
三、内置函数:
1. len(list) 长度(含有元素的数量)
示例:list = ["a", "b", "c", "d", "e"]
s = len(list)
print(s) #结果为5
2. append(所添加元素) 添加元素到集合中(必定排在最后)
示例:list = ["a", "b", "c", "d", "e"]
list.append("f")
print(list) #结果为["a", "b", "c", "d", "e", "f"]
3. insert(插入索引位置, 所添加元素) 添加元素到集合中
示例:list = ["a", "b", "c", "d", "e"]
list.insert(2, "f")
print(list) #结果为["a", "b", "f", "c", "d", "e"]
3.1. extend(集合) 继承原有集合内容到本集合中(合并两个集合)(在最后插入)
示例:list = [1, 2, 3]
list.extend[4, 5, 6]
print(list) #[1, 2, 3, 4, 5, 6]
4. pop(要删除的索引位置) 从集合中删除元素
示例:list = ["a", "b", "c", "d", "e"]
list.pop(2)
print(list) #结果为["a", "b", "d", "e"]
5. sum(list) 求和(仅限数字)
示例:list = [1, 2, 3, 4, 5]
total = sum(list)
print(total) #结果为15
6. max(list) 求最大值
示例1:list = [1, 2, 3, 4, 5]
biggest = max(list)
print(biggest) #结果为5
示例2:list = ["a", "b", "c", "d", "e"]
biggest = max(list)
print(biggest) #结果为e
7. min(list) 求最小值
示例1:list = [1, 2, 3, 4, 5]
smallest = min(list)
print(smallest) #结果为1
示例2:list = ["a", "b", "c", "d", "e"]
smallest = min(list)
print(smallest) #结果为a
8. sorted(list) 排序(默认升序)(可用于string类型元素)
示例1:list = [4, 5, 3, 1, 2]
new_list = sorted(list)
print(new_list) #结果为[1, 2, 3, 4, 5]
示例2:
list = ["sofa", "bread", "wc", "doc", "exit"]
new_list = sorted(list)
print(new_list) #结果为['bread', 'doc', 'exit', 'sofa', 'wc']
#以首字母进行排序,首字母相同时,比较第二个字母
9. sorted(list, reverse = True) 降序排序
示例1:list = [4, 5, 3, 1, 2]
new_list = sorted(list, reverse = True)
print(new_list) #结果为[5, 4, 3, 2, 1]
示例2:
list = ["sofa", "bread", "wc", "doc", "exit"]
new_list = sorted(list, reverse = True)
print(new_list) #结果为['wc', 'sofa', 'exit', 'doc', 'bread']
#以首字母进行排序,首字母相同时,比较第二个字母
10. index(元素) 获取该元素的索引
list = [4, 5, 3, 1, 2]
ind = list.index(5)
print(ind) #结果为1
#如果该元素在list集合中不存在,将会报错
10. count(元素) 统计该元素在集合中的个数
list = ['a', 'a', 'a', 'b', 'b']
i = list.count('b')
print(i) #结果为2
11. map(函数,迭代)
使用map可以避免操作list的索引
cities = ['madrid', 'paris', 'lisabon']
def up(city):
return city.upper()
m = map(up, cities)
m = list(m)
print(m)