Python List and Dictionary Methods: Tutorial with Code Examples
This tutorial introduces Python list and dictionary methods—including append, extend, pop, remove, sort, keys, values, items, get, and update—provides clear code examples for each operation, and concludes with a practice exercise that counts word frequencies in a text file.
Goal
Become proficient in using Python lists and dictionaries.
Learning Content
List methods: append, extend, pop, remove, sort.
Dictionary methods: keys, values, items, get, update.
Example Code
1. List Methods
append method
# 创建一个空列表
fruits = []
# 使用 append 方法添加元素
fruits.append("苹果")
fruits.append("香蕉")
fruits.append("橙子")
# 打印列表
print(f"水果列表: {fruits}")extend method
# 创建两个列表
list1 = [1, 2, 3]
list2 = [4, 5, 6]
# 使用 extend 方法合并列表
list1.extend(list2)
# 打印合并后的列表
print(f"合并后的列表: {list1}")pop method
# 创建一个列表
numbers = [1, 2, 3, 4, 5]
# 使用 pop 方法移除并返回最后一个元素
last_number = numbers.pop()
# 打印移除后的列表和被移除的元素
print(f"移除后的列表: {numbers}")
print(f"被移除的元素: {last_number}")remove method
# 创建一个列表
fruits = ["苹果", "香蕉", "橙子", "香蕉"]
# 使用 remove 方法移除指定元素
fruits.remove("香蕉")
# 打印移除后的列表
print(f"移除后的列表: {fruits}")sort method
# 创建一个列表
numbers = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]
# 使用 sort 方法对列表进行排序
numbers.sort()
# 打印排序后的列表
print(f"排序后的列表: {numbers}")2. Dictionary Methods
keys method
# 创建一个字典
person = {"姓名": "张三", "年龄": 25, "城市": "北京"}
# 使用 keys 方法获取所有键
keys = person.keys()
# 打印所有的键
print(f"字典的键: {list(keys)}")values method
# 创建一个字典
person = {"姓名": "张三", "年龄": 25, "城市": "北京"}
# 使用 values 方法获取所有值
values = person.values()
# 打印所有的值
print(f"字典的值: {list(values)}")items method
# 创建一个字典
person = {"姓名": "张三", "年龄": 25, "城市": "北京"}
# 使用 items 方法获取所有键值对
items = person.items()
# 打印所有的键值对
for key, value in items:
print(f"{key}: {value}")get method
# 创建一个字典
person = {"姓名": "张三", "年龄": 25, "城市": "北京"}
# 使用 get 方法获取指定键的值
name = person.get("姓名")
city = person.get("城市")
address = person.get("地址", "未知") # 如果键不存在,返回默认值
# 打印获取的值
print(f"姓名: {name}")
print(f"城市: {city}")
print(f"地址: {address}")update method
# 创建一个字典
person = {"姓名": "张三", "年龄": 25, "城市": "北京"}
# 使用 update 方法更新字典
person.update({"年龄": 26, "职业": "工程师"})
# 打印更新后的字典
print(f"更新后的字典: {person}")Practice
Write a program that counts the occurrences of each word in a text file.
# 读取文件内容
file_path = 'example.txt'
with open(file_path, 'r', encoding='utf-8') as file:
content = file.read()
# 将内容分割成单词列表
words = content.split()
# 创建一个字典来存储每个单词的出现次数
word_count = {}
# 遍历单词列表,统计每个单词的出现次数
for word in words:
if word in word_count:
word_count[word] += 1
else:
word_count[word] = 1
# 打印每个单词及其出现次数
for word, count in word_count.items():
print(f"单词 '{word}' 出现了 {count} 次。")Summary
Through this exercise you should now be comfortable with the basic operations on Python lists and dictionaries, and you have practiced applying these skills to a simple word‑frequency counting task. Further lessons will deepen your knowledge of Python data processing.
Test Development Learning Exchange
Test Development Learning Exchange
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.