• 迭代器与生成器 中


    迭代器切片

    问题

    你想得到一个由迭代器生成的切片对象,但是标准切片操作并不能做到。

    解决方案

    函数 itertools.islice() 正好适用于在迭代器和生成器上做切片操作。

    >>> def count(n):
    ...     while True:
    ...         yield n
    ...         n += 1
    ...
    >>> c = count(0)
    >>> c[10:20]
    Traceback (most recent call last):
        File "<stdin>", line 1, in <module>
    TypeError: 'generator' object is not subscriptable
    
    >>> # Now using islice()
    >>> import itertools
    >>> for x in itertools.islice(c, 10, 20):
    ...     print(x)
    ...
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    >>>
    

    迭代器和生成器不能使用标准的切片操作,因为它们的长度事先我们并不知道(并且也没有实现索引)。 函数 islice() 返回一个可以生成指定元素的迭代器,它通过遍历并丢弃直到切片开始索引位置的所有元素。 然后才开始一个个的返回元素,并直到切片结束索引位置。

    这里要着重强调的一点是 islice() 会消耗掉传入的迭代器中的数据。 必须考虑到迭代器是不可逆的这个事实。 所以如果你需要之后再次访问这个迭代器的话,那你就得先将它里面的数据放入一个列表中。

    跳过可迭代对象的开始部分

    问题

    你想遍历一个可迭代对象,但是它开始的某些元素你并不感兴趣,想跳过它们。

    解决方案

    itertools 模块中有一些函数可以完成这个任务。 首先介绍的是 itertools.dropwhile() 函数。使用时,你给它传递一个函数对象和一个可迭代对象。 它会返回一个迭代器对象,丢弃原有序列中直到函数返回Flase之前的所有元素,然后返回后面所有元素。

    为了演示,假定你在读取一个开始部分是几行注释的源文件。比如:

    >>> with open('/etc/passwd') as f:
    ... for line in f:
    ...     print(line, end='')
    ...
    ##
    # User Database
    #
    # Note that this file is consulted directly only when the system is running
    # in single-user mode. At other times, this information is provided by
    # Open Directory.
    ...
    ##
    nobody:*:-2:-2:Unprivileged User:/var/empty:/usr/bin/false
    root:*:0:0:System Administrator:/var/root:/bin/sh
    ...
    >>>
    
    如果你想跳过开始部分的注释行的话,可以这样做:
    
    >>> from itertools import dropwhile
    >>> with open('/etc/passwd') as f:
    ...     for line in dropwhile(lambda line: line.startswith('#'), f):
    ...         print(line, end='')
    ...
    nobody:*:-2:-2:Unprivileged User:/var/empty:/usr/bin/false
    root:*:0:0:System Administrator:/var/root:/bin/sh
    ...
    >>>
    

    这个例子是基于根据某个测试函数跳过开始的元素。 如果你已经明确知道了要跳过的元素的个数的话,那么可以使用 itertools.islice() 来代替。比如:

    >>> from itertools import islice
    >>> items = ['a', 'b', 'c', 1, 4, 10, 15]
    >>> for x in islice(items, 3, None):
    ...     print(x)
    ...
    1
    4
    10
    15
    >>>
    在这个例子中, islice() 函数最后那个 None 参数指定了你要获取从第3个到最后的所有元素, 如果 None 和3的位置对调,意思就是仅仅获取前三个元素恰恰相反, (这个跟切片的相反操作 [3:] 和 [:3] 原理是一样的)。
    

    排列组合的迭代

    你想迭代遍历一个集合中元素的所有可能的排列或组合

    解决方案

    itertools模块提供了三个函数来解决这类问题。 其中一个是 itertools.permutations() , 它接受一个集合并产生一个元组序列,每个元组由集合中所有元素的一个可能排列组成。 也就是说通过打乱集合中元素排列顺序生成一个元组,比如:

    >>> items = ['a', 'b', 'c']
    >>> from itertools import permutations
    >>> for p in permutations(items):
    ...     print(p)
    ...
    ('a', 'b', 'c')
    ('a', 'c', 'b')
    ('b', 'a', 'c')
    ('b', 'c', 'a')
    ('c', 'a', 'b')
    ('c', 'b', 'a')
    >>>
    如果你想得到指定长度的所有排列,你可以传递一个可选的长度参数。就像这样:
    
    >>> for p in permutations(items, 2):
    ...     print(p)
    ...
    ('a', 'b')
    ('a', 'c')
    ('b', 'a')
    ('b', 'c')
    ('c', 'a')
    ('c', 'b')
    >>>
    >
    

    使用 itertools.combinations() 可得到输入集合中元素的所有的组合。比如:

    >>> from itertools import combinations
    >>> for c in combinations(items, 3):
    ...     print(c)
    ...
    ('a', 'b', 'c')
    
    >>> for c in combinations(items, 2):
    ...     print(c)
    ...
    ('a', 'b')
    ('a', 'c')
    ('b', 'c')
    
    >>> for c in combinations(items, 1):
    ...     print(c)
    ...
    ('a',)
    ('b',)
    ('c',)
    >>>```
    对于 combinations() 来讲,元素的顺序已经不重要了。 也就是说,组合 ('a', 'b') 跟 ('b', 'a') 其实是一样的(最终只会输出其中一个)。
    
    在计算组合的时候,一旦元素被选取就会从候选中剔除掉(比如如果元素’a’已经被选取了,那么接下来就不会再考虑它了)。 而函数 itertools.combinations_with_replacement() 允许同一个元素被选择多次,比如:
    
    >>> for c in combinations_with_replacement(items, 3):
    ...     print(c)
    ...
    ('a', 'a', 'a')
    ('a', 'a', 'b')
    ('a', 'a', 'c')
    ('a', 'b', 'b')
    ('a', 'b', 'c')
    ('a', 'c', 'c')
    ('b', 'b', 'b')
    ('b', 'b', 'c')
    ('b', 'c', 'c')
    ('c', 'c', 'c')
    >>>
    >
    

    序列上索引值迭代
    问题
    你想在迭代一个序列的同时跟踪正在被处理的元素索引。

    解决方案

    内置的 enumerate() 函数可以很好的解决这个问题:

    内置的 enumerate() 函数可以很好的解决这个问题:

    >>> my_list = ['a', 'b', 'c']
    >>> for idx, val in enumerate(my_list):
    ...     print(idx, val)
    ...
    0 a
    1 b
    2 c
    

    为了按传统行号输出(行号从1开始),你可以传递一个开始参数:

    >>> my_list = ['a', 'b', 'c']
    >>> for idx, val in enumerate(my_list, 1):
    ...     print(idx, val)
    ...
    1 a
    2 b
    3 c
    

    这种情况在你遍历文件时想在错误消息中使用行号定位时候非常有用:

    def parse_data(filename):
        with open(filename, 'rt') as f:
            for lineno, line in enumerate(f, 1):
                fields = line.split()
                try:
                    count = int(fields[1])
                    ...
                except ValueError as e:
                    print('Line {}: Parse error: {}'.format(lineno, e))
    

    enumerate() 对于跟踪某些值在列表中出现的位置是很有用的。 所以,如果你想将一个文件中出现的单词映射到它出现的行号上去,可以很容易的利用 enumerate() 来完成:

    word_summary = defaultdict(list)
    
    with open('myfile.txt', 'r') as f:
        lines = f.readlines()
    
    for idx, line in enumerate(lines):
        # Create a list of words in current line
        words = [w.strip().lower() for w in line.split()]
        for word in words:
            word_summary[word].append(idx)
    
    

    同时迭代多个序列
    问题
    你想同时迭代多个序列,每次分别从一个序列中取一个元素。

    解决方案

    为了同时迭代多个序列,使用 zip() 函数。比如

    >>> xpts = [1, 5, 4, 2, 10, 7]
    >>> ypts = [101, 78, 37, 15, 62, 99]
    >>> for x, y in zip(xpts, ypts):
    ...     print(x,y)
    ...
    1 101
    5 78
    4 37
    2 15
    10 62
    7 99
    >>>
    #zip(a, b) 会生成一个可返回元组 (x, y) 的迭代器,其中x来自a,y来自b。 一旦其中某个序列到底结尾,迭代宣告结束。 因此迭代长度跟参数中最短序列长度一致。
    
    >>> a = [1, 2, 3]
    >>> b = ['w', 'x', 'y', 'z']
    >>> for i in zip(a,b):
    ...     print(i)
    ...
    (1, 'w')
    (2, 'x')
    (3, 'y')
    >>>
    #如果这个不是你想要的效果,那么还可以使用 itertools.zip_longest() 函数来代替。比如:
    
    >>> from itertools import zip_longest
    >>> for i in zip_longest(a,b):
    ...     print(i)
    ...
    (1, 'w')
    (2, 'x')
    (3, 'y')
    (None, 'z')
    
    >>> for i in zip_longest(a, b, fillvalue=0):
    ...     print(i)
    ...
    (1, 'w')
    (2, 'x')
    (3, 'y')
    (0, 'z')
    >>>
    >```
    #最后强调一点就是, zip() 会创建一个迭代器来作为结果返回。 如果你需要将结对的值存储在列表中,要使用 list() 函数。比如:
    
    >>> zip(a, b)
    <zip object at 0x1007001b8>
    >>> list(zip(a, b))
    [(1, 10), (2, 11), (3, 12)]
    >>>
    
  • 相关阅读:
    IDF2011 值得关注的东东
    用GPIO模拟SPI协议的实现[转]
    GPIO Interfaces
    电路常识性概念(8)MOS管及简单CMOS逻辑门电路原理图
    Qt's Tools
    用python 控制gpio
    高阻态
    Worker Process, Work threads, Application Pool, AppDomain, Web Site,
    Web garden 和Web farm
    MOSS2007中用户信息的同步
  • 原文地址:https://www.cnblogs.com/donghaoblogs/p/10558110.html
Copyright © 2020-2023  润新知