【Python基础知识汇总-P3列表】

发布于:2023-01-04 ⋅ 阅读:(446) ⋅ 点赞:(0)

一、What is 列表?

  • 列表由一系列 “按特定顺序排列的元素” 组成。
  • 列表用方括号 “【】” 表示,并用逗号 “,” 分隔其中的元素。
  • 列表的索引从 “0” 开始!但是在索引值为负数时,-1即返回倒数第一个元素,并以此类推。

二、列表的操作

1. 访问列表元素

#代码演示
fruits = ['apple', 'banana', 'watermelon', 'coconut']
print(fruits[0].title())
#演示结果
Apple

2. 使用列表中的值

  • 可以像使用其他变量一样,使用列表中的值。
#代码演示
fruits = ['apple', 'banana', 'watermelon', 'coconut']
message = f"我最爱吃的水果是{fruits[-1].title()}。"
print(message)
#演示结果
我最爱吃的水果是Coconut。

3. 修改列表元素

  • 先指定 “列表名” 和 “待修改元素的索引” ,再指定该元素的新值。
#代码演示
fruits = ['apple', 'banana', 'watermelon', 'coconut']
fruits[-2] = 'grape'
print(fruits)
#演示结果
['apple', 'banana', 'grape', 'coconut']

4. 在列表中添加元素

(1)在列表末尾添加元素
  • 方法:附加append()
#代码演示
fruits = ['apple', 'banana', 'watermelon', 'coconut']
fruits.append('mango')
print(fruits)
#演示结果
['apple', 'banana', 'watermelon', 'coconut', 'mango']
(2)在列表中插入元素
  • 方法:插入insert()
#代码演示
fruits = ['apple', 'banana', 'watermelon', 'coconut']
fruits.insert(1, 'pineapple')
print(fruits)
#演示结果
['apple', 'pineapple', 'banana', 'watermelon', 'coconut']

5. 从列表中删除元素

(1)del语句
  • 知道待删除元素的索引值
  • 用del语句删除列表中的元素后,该元素便无法被访问。
#代码演示
fruits = ['apple', 'banana', 'watermelon', 'coconut']
del fruits[1]
print(fruits)
#演示结果
['apple', 'watermelon', 'coconut']
(2)方法:弹出pop()
  • 删除元素,并希望接着使用它的值。eg:游戏设计中,需要获取死掉外星人的坐标,从而在坐标位置添加爆炸效果。
#代码演示
fruits = ['apple', 'banana', 'watermelon', 'coconut']
popped_fruits = fruits.pop(-1)
print(fruits)
print(f'小明最不爱吃的水果是{popped_fruits.title()}。')
#演示结果
['apple', 'banana', 'watermelon']
小明最不爱吃的水果是Coconut。
(3)方法:移除remove()
  • ① 不知道待删除元素的索引值。② 根据值来删除元素,用方法remove()
  • 方法remove()只删除第一个指定的值。如果要删除的值在列表中存在多个,需要使用循环来确保删除所有指定值。
#代码演示
fruits = ['apple', 'banana', 'watermelon', 'coconut']
too_expensive = 'watermelon'
fruits.remove(too_expensive)
print(fruits)
print(f'\n我觉得{too_expensive.title()}对我而言真贵呀。')
#演示结果
['apple', 'banana', 'coconut']

 我觉得Watermelon对我而言真贵呀。

6. 列表排序

(1)方法sort()对列表永久排序
  • 对列表的排序是永久性的,再也无法恢复到原来的顺序。
#代码演示
fruits = ['apple', 'banana', 'watermelon', 'coconut']
fruits.sort()
print(fruits)
fruits.sort(reverse=True)
print(fruits)
#演示结果
['apple', 'banana', 'coconut', 'watermelon']
['watermelon', 'coconut', 'banana', 'apple']
(2)函数sorted()对列表临时排序
  • 既可以按特定顺序呈现它们,也不影响它们在列表中的原始顺序。
#代码演示
fruits = ['apple', 'banana', 'watermelon', 'coconut']
print(sorted(fruits))
print(sorted(fruits, reverse=True))
print(fruits)
#演示结果
['apple', 'banana', 'coconut', 'watermelon']
['watermelon', 'coconut', 'banana', 'apple']
['apple', 'banana', 'watermelon', 'coconut']

7. 倒序打印列表

  • 方法reverse()
#代码演示
fruits = ['apple', 'banana', 'watermelon', 'coconut']
fruits.reverse()
print(fruits)
#演示结果
['coconut', 'watermelon', 'banana', 'apple']

8. 确定列表长度

  • 使用函数len()
#代码演示
fruits = ['apple', 'banana', 'watermelon', 'coconut']
print(len(fruits))
#演示结果
4

9. 遍历列表-循环

  • 用for循环来遍历列表,for语句末尾要加**“:”**
  • 循环内的代码,需要缩进。
#代码演示
animals = ['dog', 'cat', 'rabit']
for animal in animals:
    print(f'A {animal} would make a great pet.')
print('\nAny of these animals would make a great pet.')
#演示结果
A dog would make a great pet.
A cat would make a great pet.
A rabit would make a great pet.

Any of these animals would make a great pet.

9. 创建数值列表-函数range()

  • 使用函数range(a,b,c),其中a为起始数,b为结束数,c为步长,a和b左闭右开。

方式1:使用函数list() 将函数range() 的结果转换为列表。

#代码演示
numbers = list(range(1,11,2))
print(numbers)
#演示结果
[1, 3, 5, 7, 9]

方式2:创建空列表,再将数据附件到空列表中。

#代码演示
squares = []
for value in range(1,11):
    squares.append(value**2)
print(squares)
#演示结果
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

10. 数字列表的简单统计计算(min,max,sum等)

  • 函数min()、max()、sum()等。
#代码演示
digits = [1,11,21,24,51,100]
print(min(digits))
print(max(digits))
print(sum(digits))
#演示结果
1
100
208

11. 列表解析

  • 列表解析将for循环和创建新元素的代码合并成一行,从而在列表中自动附加新元素。
  • 模板:“列表名 = [表达式 for循环]” 其中表达式用于生成要存储到列表中的值,而for循环则用于给表达式计算提供初始值。
#代码演示
squares = [value**2 for value in range(1,11)]
print(squares)
#演示结果
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

12. 切片

  • 创建切片:列表名[起始元素:结束元素:步长],结束元素为空值时,默认切片范围从起始元素到列表最后一个元素。
#代码演示
digits = [value for value in range(1,101)]
print(digits[-10: :2])
#演示结果
[91, 93, 95, 97, 99]
  • 遍历切片:在for循环中使用切片。
#代码演示
digits = [value for value in range(1,101)]
for numbers in digits[-10: :2]:
    print(numbers)
#演示结果
91
93
95
97
99

13. 复制列表

  • 正确方式:创建一个包含整个列表的 “切片”
  • 错误方式:“新列表 = 旧列表” 此时两个变量指向同一个列表,无论哪个变量发生变化,新旧列表始终保持相同。
#代码演示
digits = [value**3 for value in range(1,4)]
new_digits = digits[ : ]
digits.append(666)
new_digits.append(888)
print(digits)
print(new_digits)
#演示结果
[1, 8, 27, 666]
[1, 8, 27, 888]

14. 元组

  • 与列表相似,是列表的特殊情况,可称为“不可变的列表”。
  • 元组用 “()”标识。本质上,元组是用 “,” 标识的,而圆括号只是让元组看起来更整洁清晰。
#代码演示
dimensions = 200, 50
for dimension in dimensions:
    print(dimension)

dimensions = (200, 50)
for dimension in dimensions:
    print(dimension)
#演示结果
200
50
200
50
  • 元组作为 “不可变的列表”,如果确实想修改元组中的值,需重新定义元组
#代码演示
dimensions = (200, 50)
print('Original dimensions:')
for dimension in dimensions:
    print(dimension)

dimensions = (400, 100)
print('\nModified dimensions:')
for dimension in dimensions:
    print(dimension)
#演示结果
Original dimensions:
200
50

Modified dimensions:
400
100
本文含有隐藏内容,请 开通VIP 后查看

网站公告

今日签到

点亮在社区的每一天
去签到