Files
Python_CookBook_repo/4.迭代器与生成器/12.在不同的容器中迭代.py
2025-09-10 16:12:45 +08:00

12 lines
497 B
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from itertools import chain
if __name__ == '__main__':
a = [1,2,3,4,5]
b = ['a','b','c','d','e']
# 正常来说我们想要迭代这两个东西需要写两个循环构建两个迭代器但这样很操蛋不如直接用chain把他俩作链表连起来
for i in chain(a, b):
print(i)
# 使用chain可以避开 a + b 要求a和b类型相同的限制同时这种操作更加快速因为其底层是用指针完成而不会创建一个新的东西