学习目标 ✅ 掌握Python动态类型特性 ✅ 熟练使用列表(List)、字典(Dict)、集合(Set)、元组(Tuple) ✅ 理解Python与Java数据结构的核心差异 ✅ 完成文本词频统计实战
特性 | Java | Python |
---|---|---|
类型声明 | 强制声明 (int x = 10;) | 自动推导 (x = 10) |
类型检查 | 编译时检查 | 运行时检查 |
类型转换 | 显式转换 ((int) 3.14) | 隐式转换 (int(3.14)) |
示例代码对比:
// Java (静态类型)
List<String> names = new ArrayList<>(); // 必须声明泛型类型
names.add("Alice");
// names.add(123); // 编译报错
# Python (动态类型)
names = ["Alice"] # 初始为字符串列表
names.append(123) # 运行时允许混合类型
print(names) # ['Alice', 123]
Python类型 | Java近似类型 | 可变性 | 示例 | 特点 |
---|---|---|---|---|
list | ArrayList | 可变 | [1, "a", True] | 有序,允许重复,支持混合类型 |
dict | HashMap | 可变 | {"name": "John"} | 键值对,快速查找 |
tuple | 无直接对应 | 不可变 | (1, "apple") | 轻量级,适合保护数据 |
set | HashSet | 可变 | {1, 2, 3} | 无序,元素唯一 |
基础操作对比:
// Java ArrayList
ArrayList<Integer> nums = new ArrayList<>();
nums.add(1);
nums.get(0);
nums.remove(0);
# Python list
nums = [1, 2, 3]
nums.append(4) # 追加元素
print(nums[0]) # 索引访问
nums.pop(0) # 删除元素
高级操作:
# 列表推导式(Java无类似语法)
squares = [x**2 for x in range(10)] # [0, 1, 4, ..., 81]
even_squares = [x**2 for x in range(10) if x % 2 == 0]
# 切片操作(比Java subList更简洁)
nums = [0,1,2,3,4,5]
print(nums[1:4]) # [1,2,3]
print(nums[::2]) # [0,2,4]
基础操作:
// Java HashMap
HashMap<String, Integer> map = new HashMap<>();
map.put("age", 30);
map.get("age");
# Python dict
user = {"name": "John", "age": 30}
user["email"] = "john@example.com" # 新增键值对
print(user.get("phone", "N/A")) # 安全获取,避免KeyError
字典推导式:
# 将列表转为字典
fruits = ["apple", "banana", "cherry"]
length_map = {fruit: len(fruit) for fruit in fruits}
# {'apple':5, 'banana':6, 'cherry':6}
a = {1,2,3}
b = {3,4,5}
print(a | b) # 并集 {1,2,3,4,5}
print(a & b) # 交集 {3}
print(a - b) # 差集 {1,2}
输入:任意英文文本 输出:单词出现频率降序排列 示例输入:
"Hello world! Hello Python. Python is awesome."
示例输出:
hello: 2
python: 2
world: 1
is: 1
awesome: 1
步骤1:文本预处理
text = "Hello world! Hello Python. Python is awesome."
# 转为小写并分割单词
words = text.lower().split()
# ['hello', 'world!', 'hello', 'python.', 'python', 'is', 'awesome.']
# 使用正则表达式去标点
import re
clean_words = [re.sub(r'[^\w]', '', word) for word in words]
# ['hello', 'world', 'hello', 'python', 'python', 'is', 'awesome']
步骤2:使用字典统计频率
word_count = {}
for word in clean_words:
if word in word_count:
word_count[word] += 1
else:
word_count[word] = 1
# 更简洁的写法
from collections import defaultdict
word_count = defaultdict(int)
for word in clean_words:
word_count[word] += 1
步骤3:结果排序输出
# 按值降序排序
sorted_words = sorted(word_count.items(), key=lambda x: x[1], reverse=True)
# 格式化输出
for word, count in sorted_words:
print(f"{word}: {count}")
import re
from collections import defaultdict
def word_frequency(text):
# 清洗数据
words = text.lower().split()
clean_words = [re.sub(r'[^\w]', '', word) for word in words]
# 统计词频
counts = defaultdict(int)
for word in clean_words:
if word: # 过滤空字符串
counts[word] += 1
# 排序输出
return sorted(counts.items(), key=lambda x: (-x[1], x[0]))
# 测试
sample_text = "Hello world! Hello Python. Python is awesome."
result = word_frequency(sample_text)
for word, count in result:
print(f"{word}: {count}")
特性 | Java | Python |
---|---|---|
空值处理 | null | None |
迭代方式 | for (int num : nums) | for num in nums: |
哈希结构 | HashMap需要指定泛型类型 | dict自动处理任意类型键值 |
内存管理 | 需要关注对象销毁 | 引用计数 + 垃圾回收机制 |
增强词频统计
列表操作挑战 使用列表推导式生成乘法表:
[[i*j for j in range(1,10)] for i in range(1,10)]
通过第二天的学习,您将深入掌握Python核心数据结构的使用技巧,并理解与Java的核心差异。关键要点: 1️⃣ Python的动态类型带来灵活性,但也需要更谨慎的类型处理 2️⃣ 列表推导式等语法糖能极大提升开发效率 3️⃣ 字典是Python最强大的数据结构之一,善用可以简化复杂逻辑
本篇的分享就到这里了,感谢观看,如果对你有帮助,别忘了点赞+收藏+关注。