python的迭代器模块

import itertools
"""
itertools 是python的迭代器模块,itertools提供的生成迭代器的函数,
相当高效且节省内存。使用这些工具,你将能够创建自己定制的迭代器用于高效率的循环。
"""
l = [[0], [1, 2], [2], [3, 6], [4], [5, 10]]
print(list(itertools.chain.from_iterable(l)))   # [0, 1, 2, 2, 3, 6, 4, 5, 10]

country_and_countrycodes = [('United States', 'US'), ('China', 'CH')]
all_countries = ['United States', 'Mongolia', 'Togo']
print(set(itertools.chain.from_iterable(country_and_countrycodes))) # 供了元组的固定集合
diedia = itertools.chain.from_iterable(country_and_countrycodes) # 当返回给你内存地址咋办,有可能返回的是迭代器,需要迭代输出
for i in diedia:
    print(i) # 打印出来的字符并没有加引号
努力拼搏吧,不要害怕,不要去规划,不要迷茫。但你一定要在路上一直的走下去,尽管可能停滞不前,但也要走。
原文地址:https://www.cnblogs.com/wkhzwmr/p/15063298.html