高效合并、计数与迭代

高效编写 Python 代码

Logan Thomas

Scientific Software Technical Trainer, Enthought

宝可梦概览

  • 训练家(收集宝可梦)

alt="小智;任天堂宝可梦游戏中的一位训练家"

高效编写 Python 代码

宝可梦概览

  • 宝可梦(虚构的动物角色)

alt="杰尼龟、皮卡丘、妙蛙种子和小火龙;任天堂宝可梦游戏中的部分宝可梦"

高效编写 Python 代码

宝可梦概览

  • 图鉴(存储捕获的宝可梦)

alt="图鉴,训练家用于存储捕获宝可梦的工具"

高效编写 Python 代码

宝可梦简介

alt="名为杰尼龟的宝可梦及其元数据"

高效编写 Python 代码

宝可梦简介

alt="名为杰尼龟的宝可梦及其元数据,突出显示名称和世代字段"

高效编写 Python 代码

宝可梦简介

alt="名为杰尼龟的宝可梦及其元数据,突出显示类型和传说字段"

高效编写 Python 代码

宝可梦简介

alt="名为杰尼龟的宝可梦及其元数据,突出显示生命值、攻击、防御、特攻、特防、速度和总和字段"

高效编写 Python 代码

合并对象

names = ['Bulbasaur', 'Charmander', 'Squirtle']
hps = [45, 39, 44]
combined = []

for i,pokemon in enumerate(names):
    combined.append((pokemon, hps[i]))

print(combined)
[('Bulbasaur', 45), ('Charmander', 39), ('Squirtle', 44)]
高效编写 Python 代码

用 zip 合并对象

names = ['Bulbasaur', 'Charmander', 'Squirtle']
hps = [45, 39, 44]
combined_zip = zip(names, hps)

print(type(combined_zip))
<class 'zip'>
combined_zip_list = [*combined_zip]

print(combined_zip_list)
[('Bulbasaur', 45), ('Charmander', 39), ('Squirtle', 44)]
高效编写 Python 代码

collections 模块

  • Python 标准库的一部分(内置模块)
  • 专用容器数据类型
    • 通用 dict、list、set、tuple 的替代方案
  • 重要:
    • namedtuple:带命名字段的元组子类
    • deque:类列表容器,追加与弹出更快
    • Counter:用于统计可哈希对象的字典
    • OrderedDict:保持条目顺序的字典
    • defaultdict:缺失值由工厂函数提供的字典
高效编写 Python 代码

collections 模块

  • Python 标准库的一部分(内置模块)
  • 专用容器数据类型
    • 通用 dict、list、set、tuple 的替代方案
  • 重要:
    • namedtuple:带命名字段的元组子类
    • deque:类列表容器,追加与弹出更快
    • Counter:用于统计可哈希对象的字典
    • OrderedDict:保持条目顺序的字典
    • defaultdict:缺失值由工厂函数提供的字典
高效编写 Python 代码

用循环计数

# 每只宝可梦的类型(共 720)
poke_types = ['Grass', 'Dark', 'Fire', 'Fire', ...]

type_counts = {}
for poke_type in poke_types: if poke_type not in type_counts: type_counts[poke_type] = 1 else: type_counts[poke_type] += 1
print(type_counts)
{'Rock': 41, 'Dragon': 25, 'Ghost': 20, 'Ice': 23, 'Poison': 28, 'Grass': 64,
 'Flying': 2, 'Electric': 40, 'Fairy': 17, 'Steel': 21, 'Psychic': 46, 'Bug': 65,
 'Dark': 28, 'Fighting': 25, 'Ground': 30, 'Fire': 48,'Normal': 92, 'Water': 105}
高效编写 Python 代码

collections.Counter()

# 每只宝可梦的类型(共 720)
poke_types = ['Grass', 'Dark', 'Fire', 'Fire', ...]

from collections import Counter
type_counts = Counter(poke_types)
print(type_counts)
Counter({'Water': 105, 'Normal': 92, 'Bug': 65, 'Grass': 64, 'Fire': 48,
         'Psychic': 46, 'Rock': 41, 'Electric': 40, 'Ground': 30,
         'Poison': 28, 'Dark': 28, 'Dragon': 25, 'Fighting': 25, 'Ice': 23,
         'Steel': 21, 'Ghost': 20, 'Fairy': 17, 'Flying': 2})
高效编写 Python 代码

itertools 模块

  • Python 标准库的一部分(内置模块)
  • 创建与使用迭代器的函数式工具
  • 重要:
    • 无限迭代器:countcyclerepeat
    • 有限迭代器:accumulatechainzip_longest
    • 组合生成器:productpermutationscombinations
高效编写 Python 代码

itertools 模块

  • Python 标准库的一部分(内置模块)
  • 创建与使用迭代器的函数式工具
  • 重要:
    • 无限迭代器:countcyclerepeat
    • 有限迭代器:accumulatechainzip_longest
    • 组合生成器:productpermutationscombinations
高效编写 Python 代码

用循环生成组合

poke_types = ['Bug', 'Fire', 'Ghost', 'Grass', 'Water']

combos = [] for x in poke_types: for y in poke_types: if x == y: continue if ((x,y) not in combos) & ((y,x) not in combos): combos.append((x,y))
print(combos)
[('Bug', 'Fire'), ('Bug', 'Ghost'), ('Bug', 'Grass'), ('Bug', 'Water'),
 ('Fire', 'Ghost'), ('Fire', 'Grass'), ('Fire', 'Water'),
 ('Ghost', 'Grass'), ('Ghost', 'Water'), ('Grass', 'Water')]
高效编写 Python 代码

itertools.combinations()

poke_types = ['Bug', 'Fire', 'Ghost', 'Grass', 'Water']

from itertools import combinations
combos_obj = combinations(poke_types, 2)
print(type(combos_obj))
<class 'itertools.combinations'>
combos = [*combos_obj]
print(combos)
[('Bug', 'Fire'), ('Bug', 'Ghost'), ('Bug', 'Grass'), ('Bug', 'Water'),
 ('Fire', 'Ghost'), ('Fire', 'Grass'), ('Fire', 'Water'),
 ('Ghost', 'Grass'), ('Ghost', 'Water'), ('Grass', 'Water')]
高效编写 Python 代码

Passons à la pratique !

高效编写 Python 代码

Preparing Video For Download...