2 列表(list)
参考https://www.runoob.com/python3/python3-list.html
特点: 可以进行的操作包括索引,切片,加,乘,检查成员。
1 创建
a.创建空列表
num = []
print(num)
print(len(num))
输出
[]
0
b.创建有内容的列表
list1 = ['Google', 'Runoob', 1997, 2000]
print(list1)
print(len(list1))
输出
['Google', 'Runoob', 1997, 2000]
4
c.使用函数**range()**创建数字列表
for value in range(1, 5):#range函数示例
print(value)
numbers = list(range(1, 6))#函数list() 将range() 的结果直接转换为列表
print(numbers)
even_numbers = list(range(2, 11, 2))#指定步长2
print(even_numbers)
输出
1
2
3
4
[1, 2, 3, 4, 5]
[2, 4, 6, 8, 10]
d.创建切片
players = ['charles', 'martina', 'michael', 'florence', 'eli']
print(players[0:3])
输出
['charles', 'martina', 'michael']
2 访问列表中的值
a.单值访问(超出范围报错)
①正向索引
list = ['red', 'green', 'blue', 'yellow', 'white', 'black']
print( list[0] )#从 0 开始
输出
red
②反向索引
list = ['red', 'green', 'blue', 'yellow', 'white', 'black']
print( list[-1] )#从尾部开始,最后一个元素的索引为 -1,往前一位为 -2
输出
black
b.两端索引截取列表切片(超出范围不报错)
①正向截取不超范围
nums = [10, 20, 30, 40, 50, 60, 70, 80, 90]
print(nums[0:4])
输出
[10, 20, 30, 40]
②反向截取仍从左到右截取
nums = [10, 20, 30, 40, 50, 60, 70, 80, 90]
print(nums[-3:-1])
输出
[70, 80]
③超范围截取
i.
nums = [10, 20, 30, 40, 50, 60, 70, 80, 90]
print(nums[1:11])
输出
[20, 30, 40, 50, 60, 70, 80, 90]
ii.
nums = [10, 20, 30, 40, 50, 60, 70, 80, 90]
print(nums[11:-1])
输出
[]
c.一端索引截取列表切片
①
nums = [10, 20, 30, 40, 50, 60, 70, 80, 90]
print(nums[3:])
输出
[40, 50, 60, 70, 80, 90]
②
nums = [10, 20, 30, 40, 50, 60, 70, 80, 90]
print(nums[-3:])
nums = [10, 20, 30, 40, 50, 60, 70, 80, 90]
print(nums[:3])
nums = [10, 20, 30, 40, 50, 60, 70, 80, 90]
print(nums[:-3])
d.两端无索引切片
my_foods = ['pizza', 'falafel', 'carrot cake']
friend_foods = [1,2,3,4,5,6]
friend_foods = my_foods[:]
print(my_foods)
print(friend_foods)
输出
['pizza', 'falafel', 'carrot cake']
['pizza', 'falafel', 'carrot cake']
e.遍历
magicians = ['alice', 'david', 'carolina']
for magician in magicians:
print(magician)
print('\n')
for magician in magicians[:2]:#遍历切片
print(magician.title())
输出
alice
david
carolina
Alice
David
3 更新列表
a.直接赋予新列表
list = ['Google', 'Runoob', 1997, 2000]
list[2] = 2001
print(list)
输出
['Google', 'Runoob', 2001, 2000]
b.使用append()方法在末尾增加
list1 = ['Google', 'Runoob', 'Taobao']
list1.append('Baidu')
print ("更新后的列表 : ", list1)
输出
更新后的列表 : ['Google', 'Runoob', 'Taobao', 'Baidu']
c.使用insert()方法,可指定新元素的索引和值
motorcycles = ['honda', 'yamaha', 'suzuki']
motorcycles.insert(0, 'ducati')
print(motorcycles)
输出
['ducati', 'honda', 'yamaha', 'suzuki']
4 删除列表元素
a.del语句通过特定索引删除
list = ['Google', 'Runoob', 1997, 2000]
print (list)
del list[2]
print (list)
输出
['Google', 'Runoob', 1997, 2000]
['Google', 'Runoob', 2000]
b.remove()方法通过值删除第一个匹配项
list1 = ['Google', 'Runoob', 'Taobao', 'Baidu','Google']
print (list1)#原本的
list1.remove('Taobao')#第一次删
print (list1)
list1.remove('Google')#第二次删
print (list1)
输出
['Google', 'Runoob', 'Taobao', 'Baidu', 'Google']
['Google', 'Runoob', 'Baidu', 'Google']
['Runoob', 'Baidu', 'Google']
c.pop()方法弹出元素
i.
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
popped_motorcycle = motorcycles.pop()#储存弹出的元素,无参数时弹出末尾元素
print(popped_motorcycle)
print(motorcycles)
输出
['honda', 'yamaha', 'suzuki']
suzuki
['honda', 'yamaha']
ii.
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
popped_motorcycle = motorcycles.pop(0)#弹出特定索引的元素
print(popped_motorcycle)
print(motorcycles)
输出
['honda', 'yamaha', 'suzuki']
honda
['yamaha', 'suzuki']
5 Python列表脚本操作符
a. + 组合列表
squares = [1, 4, 9, 16, 25]
squares += [36, 49, 64, 81, 100]
print(squares)
输出
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
b. * 重复列表
list1 = ['Hi!'] * 4
print(list1)
输出
['Hi!', 'Hi!', 'Hi!', 'Hi!']
6 嵌套列表
a = ['a', 'b', 'c']
n = [1, 2, 3]
x = [a, n]
print(x)
print(x[0])#a,n可以不是列表
print(x[0][1])
输出
[['a', 'b', 'c'], [1, 2, 3]]
['a', 'b', 'c']
b
7 列表比较
import operator
a = [1, 2]
b = [2, 3]
c = [2, 3]
print("operator.eq(a,b): ", operator.eq(a,b))
print("operator.eq(c,b): ", operator.eq(c,b))
输出
operator.eq(a,b): False
operator.eq(c,b): True
8 组织列表
a.使用方法**sort()**对列表永久排序
cars = ['bmw', 'audi', 'toyota', 'subaru']
cars.sort()#字符串按序
print(cars)
cars = ['bmw', 'audi', 'toyota', 'subaru']
cars.sort(reverse=True)#字符串反序
print(cars)
nums = [1214,-124,78]
nums.sort()
print(nums)
输出
['audi', 'bmw', 'subaru', 'toyota']
['toyota', 'subaru', 'bmw', 'audi']
[-124, 78, 1214]
b.对列表临时排序,方法sorted()
cars = ['Bmw', 'audi', 'toyota', 'subaru']
print(cars)
print(sorted(cars))
print(sorted(cars,reverse=True))
print(cars)
输出
['Bmw', 'audi', 'toyota', 'subaru']
['Bmw', 'audi', 'subaru', 'toyota']
['toyota', 'subaru', 'audi', 'Bmw']
['Bmw', 'audi', 'toyota', 'subaru']
9 对数字列表统计计算
digits = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
print(min(digits))
print(max(digits))
print(sum(digits))
输出
0
9
45
10 列表解析
squares = [value**2 for value in range(1, 11)]
print(squares)
#解析:在这个示例中,表达式为value**2 ,它计算平方值。接下来,编写一个for 循环,用于给表达式提供值
输出
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]