Skip to content

列表

列表遍历

  1. 使用index索引遍历列表。
python
numbers = [12,34,30,76,18]
for index in range(0, len(numbers)):
    print(numbers[index])
  1. 使用for直接遍历列表元素(item)。
python
numbers = [12,34,30,76,18]
for item in numbers:
    print(item)

3.得到一个新列表,列表中所有元素翻倍

python
numbers = [12,34,30,76,18]
numbers1 = numbers
for index in range(0, len(numbers)):
    numbers1[index] = numbers[index] * 2
   
print(numbers1)

列表筛选

  1. 给定列表,筛选出其中所有偶数,生成一个新的偶数列表并输出。
python
numbers = [12, 34, 45, 9, 8, 90, 3, 17, 26]
even_numbers = []  # 初始化新列表
# 遍历原列表,筛选偶数
for num in numbers:
    if num % 2 == 0:
        even_numbers.append(num)

print("原列表:", numbers)
print("筛选后的偶数列表:", even_numbers)  # 输出 [12, 34, 8, 90, 26]

2.筛选列表中 “能被 3 整除且大于 10” 的元素

python
numbers = [5, 12, 7, 18, 21, 9, 27, 4, 33]
filtered_sorted = []
# 1. 筛选条件:能被 3 整除且大于 10
for num in numbers:
    if num % 3 == 0 and num > 10:
        filtered_sorted.append(num)

print("筛选后的列表:", filtered_sorted)

3.列表元素去重(保留首次出现的元素)

python
nums = [1, 2, 3, 2, 1, 4, 5, 4, 6]
unique_nums = []  # 存储无重复元素的新列表

# 遍历原列表,只添加首次出现的元素
for num in nums:
    if num not in unique_nums:
        unique_nums.append(num)

print("原列表:", nums)
print("去重后的列表:", unique_nums)  # 输出 [1, 2, 3, 4, 5, 6]
  1. 列表元素反转: 给定列表如 letters = ["a", "b", "c", "d", "e"],生成新的反转列表["e","d","c","b","a"]。
python
letters = ["a", "b", "c", "d", "e"]
reversed_letters = []  # 存储反转后的列表
length = len(letters)  # 获取列表长度

# 从最后一个元素开始遍历,添加到新列表
for i in range(length - 1, -1, -1):
    reversed_letters.append(letters[i])

print("原列表:", letters)
print("反转后的列表:", reversed_letters)  # 输出 ["e", "d", "c", "b", "a"]

列表聚合计算(描述性统计)如求和,求平均值,求最大值,最小值等

  1. 用 for 循环遍历列表元素,统计元素个数、计算总和。
python
# 遍历数字列表,计算总和和平均值
scores = [85, 92, 78, 90, 88]
total = 0
count = 0

for score in scores:
    total += score  # 累加分数
    count += 1  # 统计个数

average = total / count
print("分数列表:", scores)
print("总分:", total)  # 输出 433
print("平均分:", average)  # 输出 86.6

2.统计列表中某个元素的出现次数

python
fruits = ["苹果", "香蕉", "橙子", "苹果", "葡萄", "苹果", "香蕉"]
apple_count = 0  # 计数器

# 遍历列表,统计苹果出现次数
for fruit in fruits:
    if fruit == "苹果":
        apple_count += 1

print("列表:", fruits)
print("苹果出现的次数:", apple_count)  # 输出 3
  1. 计算列表中所有非负元素的平均值(筛选加聚合)
python
scores = [85, -92, 78, -90, 88, 95, -7]
non_negative = []  # 存储非负元素
total = 0  # 非负元素总和
count = 0  # 非负元素个数

# 筛选非负元素并累加
for score in scores:
    if score >= 0:
        non_negative.append(score)
        total += score
        count += 1

# 计算平均值(处理无正元素的情况)
if count == 0:
    average = 0
else:
    average = total / count

print("原列表:", scores)
print("非负元素列表:", non_negative)  # 输出 [85, 78, 88, 95]
print("非负元素平均值:", average)  # 输出 86.5

4.查找列表中最大值和所有索引

python
answer = []
score = [85,92,78,92,88,92,7,19]
maxNum = score[0]
for index in range(0,len(score)):
    if score[index] > maxNum:
        maxNum = score[index] 
         
print(maxNum)

for index in range(0,len(score)):
    if maxNum == score[index]:
        answer.append(index)
print(answer)

列表增、删、改

  1. 用 append()、insert() 新增元素,del、remove() 删除元素,直接赋值修改元素。
python

fruits = ["苹果", "香蕉", "橙子"]
# 1. 新增元素
fruits.append("葡萄")  # 末尾添加
print("添加葡萄后:", fruits)  # 输出 ['苹果', '香蕉', '橙子', '葡萄']
fruits.insert(1, "草莓")  # 索引 1 位置插入
print("插入草莓后:", fruits)  # 输出 ['苹果', '草莓', '香蕉', '橙子', '葡萄']

# 2. 修改元素
fruits[2] = "芒果"  # 把索引 2 的元素改成芒果
print("修改后:", fruits)  # 输出 ['苹果', '草莓', '芒果', '橙子', '葡萄']

# 3. 删除元素
del fruits[3]  # 删除索引 3 的元素(橙子)
print("删除索引 3 后:", fruits)  # 输出 ['苹果', '草莓', '芒果', '葡萄']
fruits.remove("草莓")  # 删除指定值(草莓)
print("删除草莓后:", fruits)  # 输出 ['苹果', '芒果', '葡萄']

基于 VitePress 构建的 AP CSP 学习平台