print(apply_twice(lambda x: x + 1, 0)) # 2 print(apply_twice(lambda x: x * 2, 3)) # 12
1.3 map / filter / reduce:三剑客
flowchart LR
A["📥 输入<br/>[1,2,3,4,5,6]"] --> B["🔄 map"]
A --> C["🔍 filter"]
A --> D["🔣 reduce"]
B --> B1["📤 [2,4,6,8,10,12]<br/>x*2"]
C --> C1["📤 [2,4,6]<br/>x%2==0"]
D --> D1["📤 21<br/>累加求和"]
style A fill:#C7CEEA,stroke:#9FA8DA,color:#333
style B fill:#E8D5F5,stroke:#CE93D8,color:#333
style C fill:#E8D5F5,stroke:#CE93D8,color:#333
style D fill:#E8D5F5,stroke:#CE93D8,color:#333
style B1 fill:#B5EAD7,stroke:#80CBC4,color:#333
style C1 fill:#B5EAD7,stroke:#80CBC4,color:#333
style D1 fill:#B5EAD7,stroke:#80CBC4,color:#333
flowchart TD
A["itertools"] --> B["chain / chain.from_iterable"]
A --> C["islice / islice"]
A --> D["cycle / count"]
A --> E["accumulate / reduce 兄弟"]
A --> F["groupby"]
A --> G["tee / compress"]
B --> B1["拼接多个迭代器"]
C --> C1["切片迭代器"]
D --> D1["无限循环"]
E --> E1["累积计算"]
F --> F1["分组"]
G --> G1["复制 / 选择性过滤"]
style A fill:#FFB3C6,stroke:#F48FB1,color:#333
style B fill:#C7CEEA,stroke:#9FA8DA,color:#333
style C fill:#B5EAD7,stroke:#80CBC4,color:#333
style D fill:#FFDAB9,stroke:#FFAB76,color:#333
style E fill:#E8D5F5,stroke:#CE93D8,color:#333
style F fill:#FFDAB9,stroke:#FFAB76,color:#333
style G fill:#C7CEEA,stroke:#9FA8DA,color:#333
# 实用技巧:结合 itertools.count 实现复杂过滤 # 过滤所有质数位置上的元素 defis_prime(n): if n < 2: returnFalse for i inrange(2, int(n**0.5) + 1): if n % i == 0: returnFalse returnTrue
defcompose(*funcs: Callable) -> Callable: """ 函数组合: compose(f, g, h)(x) = f(g(h(x))) 从右到左依次执行 """ defcomposed(x): result = x for f inreversed(funcs): # 反转:先执行最右边的函数 result = f(result) return result return composed
# 示例:文本处理 pipeline strip = lambda s: s.strip() lower = lambda s: s.lower() remove_punct = lambda s: ''.join(c for c in s if c.isalnum() or c.isspace())
defextract_features(text: str) -> dict: """从文本提取多维特征""" words = text.lower().split() chars = list(text) return { "raw_length": len(text), "word_count": len(words), "unique_words": len(set(words)), "avg_word_len": sum(len(w) for w in words) / max(len(words), 1), "char_types": { "alpha": sum(c.isalpha() for c in chars), "digit": sum(c.isdigit() for c in chars), "space": sum(c.isspace() for c in chars), } }
# 批量处理 texts = [ "Hello world, this is a test", "Python is awesome! 123", "AI and machine learning are transforming technology", ]
# 使用 map 批量提取特征 features = list(map(extract_features, texts))
# 使用 itertools.groupby 按特征分组 # 例如:按词数分组 sorted_by_words = sorted(features, key=lambda f: f["word_count"]) for length, group in itertools.groupby(sorted_by_words, key=lambda f: f["word_count"]): items = list(group) print(f"Word count {length}: {len(items)} texts")
@functools.lru_cache(maxsize=1024) defcached_llm_call(prompt: str) -> str: """带缓存的 LLM 调用,自动去重""" # 模拟实际 LLM API 调用 returnf"Response to: {prompt[:50]}..."
# 模拟多次相同请求(常见于 RAG 检索结果有重复) prompts = [ "What is AI?", "What is machine learning?", "What is AI?", # 重复 "What is deep learning?", "What is AI?", # 再次重复 ]
# 直接调用:重复请求会命中缓存 for p in prompts: result = cached_llm_call(p) print(f"Prompt: {p[:20]}... -> {result[:30]}...")