第三章 列表
第1讲
1.列表的定义
列表是以[]括起来的多个值;
列表中的每个值叫元素或项;
family = ["mom","dad","baby"] names = ["Ann","lily"] nums = [1,2,3,4] print(family) print(names) print(nums) 结果: ['mom', 'dad', 'baby'] ['Ann', 'lily'] [1, 2, 3,4]
2.索引
索引从左开始,为正数;
索引从右开始,为负数;
nums = [1,2,3,4] # 从左往右,为正数开始 print(nums[0]) print(nums[1]) print(nums[2]) print(nums[3]) print("===========================") # 从右往左,为负数开始 print(nums[-1]) print(nums[-2]) print(nums[-3]) print(nums[-4]) 结果: 1 2 3 4 =========================== 4 3 2 1
3.索引的综合案例
# 根据输入的年月日以数字形式打印日期 # months列表用来存储12个月 months = ["January","February",'March',"April","May","June", "July","August","September","October","November","December"] days = ["st","nd","rd"] + 17 * ["th"] \ + ["st","nd","rd"] + 7 * ["th"]\ + ["st"] year = input("year is:") # 由于input接收的为str类型,需要通过int函数转化为整型 month = input("month is (1-12):") day = input("day is (1-31):") month_num = int(month) day_num = int(day) month_name = months[month_num-1] ordinal = day + days[day_num-1] print(month_name + "-" + ordinal + "-" + year)
第2讲: 列表的分片与成员资格
1、分片
取一定范围内的元素(项)
注意:
-
- 片的结束点索引针对的元素无法取到
- 从开始点的元素(最左边)包括在结果之中,而结束点(最右边的元素nums[1:4]则不包 含在分片之内
- 当时用1个负数为步长时,必须让开始点(索引)大于结束点
- 没有制定开始点(索引)与结束点,正数步长会从序列的头部开始向右提取元素,而步长 为负数的,则从序列的尾部开始向左提取元素。
# nums列表中定义了10个元素(项) nums = [0,1,2,3,4,5,6,7,8,9] print(nums[0]) print(nums[1]) print(nums[-1]) print("============切片=============") print(nums[0:3]) # 取索引0<=x<4 print(nums[4:7]) # 取索引4<=x<7 print(nums[-3:-1]) # 取-3<x=<-1 print(nums[-3:]) print(nums[7:10]) # 取7<=x<10 print("============步长=============") print(nums[0:10:2]) print(nums[::2]) print(nums[::-2]) print(nums[-1:-10:-2])
结果:
0
1
9
============切片=============
[0, 1, 2]
[4, 5, 6]
[7, 8]
[7, 8, 9]
[7, 8, 9]
============步长=============
[0, 2, 4, 6, 8]
[0, 2, 4, 6, 8]
[9, 7, 5, 3, 1]
[9, 7, 5, 3, 1]
2.列表的相乘与相加
num1 = [1,2,3] num2 = [4,5,6] print(num1 + num2) str1 = "hello" str2 = "world" print(str1 + str2) #print(num1 + str1) # 列表与字符串无法相加 a = "python" print(a * 5) print(["python"]* 5) print(["python","java"]* 5)
结果:
[1, 2, 3, 4, 5, 6]
helloworld
pythonpythonpythonpythonpython
['python', 'python', 'python', 'python', 'python']
['python', 'java', 'python', 'java', 'python', 'java', 'python', 'java', 'python', 'java']
3.成员资格变量(in)
注意:input函数接受到的为str类型
# Demo 成员资格变量in student_info = [ ["Bela",23], ["Leo",12] ] username = input("The student name is:") age = int(input("The student age is:")) if [username,age]in student_info: print(True) else: print(False)
结果:
The student name is:Bela
The student age is:23
True
4.列表的长度、最大值、最小值
nums = [0,1,2,5,4] print("The nums_list lenth is:",len(nums)) print("The nums_list max value is:",max(nums)) print("The nums_list min value is:",min(nums)) print("=============字符串============") str = ["Alice","alen","Bob","box","Dlen","Cater"] print("The nums_list lenth is:",len(str)) print("The nums_list max value is:",max(str)) print("The nums_list min value is:",min(str)) 结果: The nums_list lenth is: 5 The nums_list max value is: 5 The nums_list min value is: 0 =============字符串============ The nums_list lenth is: 6 The nums_list max value is: box The nums_list min value is: Alice
第3 讲:列表的操作
1.list函数: 将字符串转化为列表
name = "Bob" print(name) print(list(name)) 结果: Bob ['B', 'o', 'b']
2.列表中元素的维护
增加元素(append增加1个元素,extend增加多个元素)、删除元素(remove后面跟元素值、del跟索引值)、插入元素(insert)、弹出元素(pop)
# 列表中元素的维护、增、删、改 x = [1,2,3] print(x) # 修改列表元素(项) x[1] = 5 print(x) # 给列表增加元素 print("===========增加元素===========") names = ["Lily","Lucy","Ann"] print(names) names.append("Bill") print(names) names.append(["Bill","Bily"]) print(names) names.append("Alice") print(names) names.extend(["Jojo","Apple"]) print(names) # 插入元素 print("===========插入元素===========") info = ["name","sex"] print(info) info.insert(1,"age") print(info) # 给列表删除元素 print("===========删除元素===========") str = ["Lily","Lucy","Ann"] print(str) str.remove("Ann") # remove通过值移除元素 print(str) del str[1] # del print(str) # 弹出元素 print("===========弹出元素===========") student_name = ["Lily","Lucy","Ann"] print(student_name) student_name.pop() print(student_name) student_name.pop(0) print(student_name) 结果: [1, 2, 3] [1, 5, 3] ===========增加元素=========== ['Lily', 'Lucy', 'Ann'] ['Lily', 'Lucy', 'Ann', 'Bill'] ['Lily', 'Lucy', 'Ann', 'Bill', ['Bill', 'Bily']] ['Lily', 'Lucy', 'Ann', 'Bill', ['Bill', 'Bily'], 'Alice'] ['Lily', 'Lucy', 'Ann', 'Bill', ['Bill', 'Bily'], 'Alice', 'Jojo', 'Apple'] ===========插入元素=========== ['name', 'sex'] ['name', 'age', 'sex'] ===========删除元素=========== ['Lily', 'Lucy', 'Ann'] ['Lily', 'Lucy'] ['Lily'] ===========弹出元素=========== ['Lily', 'Lucy', 'Ann'] ['Lily', 'Lucy'] ['Lucy']
3.列表的分片赋值
lstr = ["a","b","c"] print(lstr) # 分片追加(原索引位置的值,被替换) lstr[2:] = list("de") print(lstr) # 分片插入 lstr[2:2] = "f" print(lstr) lstr[2:4] = ["g","h"] print(lstr) # 删除元素 lstr[2:] = "" print(lstr) 结果: ['a', 'b', 'c'] ['a', 'b', 'd', 'e'] ['a', 'b', 'f', 'd', 'e'] ['a', 'b', 'g', 'h', 'e'] ['a', 'b']
4.搜索元素
names = ["Alice","Bob","Tom"] # 根据元素在列表中搜索 if "alice" in names: print(True) else: print(False) # 根据索引的值 print(names.index("Bob")) 结果: False 1
第4讲 列表的操作(count、sort等)
1.count():计算列表中某元素出现的次数
reverse():将元素倒序排列
# 列表的操作count、reverse nums = [1,2,3,4,5,1] names = ["Alice","Bob","Tom"] print(nums.count(5)) print(names.count("bob")) # reverse()讲列表的元素反向存放 nums.reverse() print(nums) names.reverse() print(names)
2.sort与sorted
sort针对自身列表进行排序,因此返回的是None
sorted对原列表不进行任何更改
numbers = [1,2,3,4,6,5] print(numbers) numbers.sort() print(numbers) print("===========sort返回None============") x = [1,2,3,5,4] y = x.sort() # x.sort()返回的是空值None print(x) print(y) print("===========sort返回None的优化============") x1 = [1,2,3,5,4] y1 = x1 x1.sort() print(x1) print(y1) print("===========sorted===========") x2 = [1,2,3,5,4] y2 = sorted(x2) print(x2) print(y2)
第5讲:元祖
1.元祖:不可改变的列表叫做元祖,通过()表示,列表是[]表示
2. tuple函数:可以将字符串或列表转化为元祖
# 通过list函数可将字符串转化为列表 name = "Bob" print(list(name)) # tuple可以将字符串或列表转化为元祖 info = "information" print(tuple(info)) names = ["Alice","Bob"] print(tuple(names)) print(tuple(names[1])) # 将列表中某个转化为元祖
3.双重列表
student_info = [["Alice",1001],["Bob",1002],["Tome",1003]] print("==========student_info中每个元素的值=========") for student in student_info: print(student) print("==========student_info中单个元素的值或子列表的值=========") print(student_info[0]) print(student_info[1][1])