python 学习汇总27:itertools函数详解( tcy)

itertools函数 2018/11/14

2.1.创建新iter:
count(start=0, step=1)#无限循环数;按Ctrl + C退出
# 返回均匀间隔值无限流;通常用作map()生成连续数据点的参数。此外,用于zip()添加序列号
g = itertools.count(10,2) --> 10 12 14...

cycle(iter)#对象重复循环
# 保存提供迭代器内容副本并返回一个新的迭代器,该迭代器从头到尾返回其元素。新的迭代器将无限重复这些元素
g = itertools.cycle('ABCD') --> A B C D A B CD ...

repeat(elem [,n]) #无休止或最多n次
g = itertools.repeat(10, 3) --> 10 10 10

# 重复使用的一个常见用途是为映射 或压缩提供一个常量值流:
list(map(pow, range(10), repeat(2))) # [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

chain(iterA, iterB, ...) #迭代对象串联,形成一个更大迭代器
g=itertools.chain(['a', 'b', 'c'], (1, 2, 3)) =>a, b, c, 1, 2, 3

classmethod chain.from_iterable(iterable )#替代构造函数chain()
#从单个迭代参数中获取链接的输入,这个参数是懒惰评估的

itertools.islice(iter, start=0, stop, step=1)#类似切片,参数不能为负
# 用于从内部结构扁平化的数据中提取相关字段

itertools.islice(range(10), 2, 8, 2) =>2, 4, 6
tertools.islice('ABCDEFG', 2, None) --> C D E F G

tee(iter, n=2)#返回n个独立迭代器
#迭代器很大并且其中一个新迭代器消耗大于一个,会消耗大量内存。
itertools.tee( itertools.count() ) =>iterA, iterB
# iterA -> 0, 1, 2, 3, ...;iterB ->0, 1, 2, 3,...

zip_longest(* iterables,fillvalue = None )#迭代器聚合
# 聚合来自每个迭代器的元素。如果迭代的长度不均匀,则使用fillvalue填充缺失值。
zip_longest('ABCD', 'xy', fillvalue='-') --> Ax By C- D- 

2.2.迭代器用作函数参数 

 itertools.starmap

itertools.starmap(func, iter)#根据条件(func)映射全部元素
# 迭代器返回一组元组,并用元组作为参数调用func:
    用途:
        # operator模块包含运算符函数。
        # operator.add(a, b)operator.ne(a, b)a !=boperator.attrgetter('id').id
    starmap和map区别:
        # starmap()可用于无穷序列,两序列长度不一致以短的为准;返回迭代对象;
        # 而map()返回list。当你调用map()时,已经计算完毕
    实例:

    g= itertools.starmap(lambda x: x * x, [1, 2, 3])# g只是一个迭代对象
    # 必须用for循环对g进行迭代,才会在每次循环过程中计算出下一个元素:
    for x in g:
    print(x)  #1 4 9

    r = map(lambda x: x * x, [1, 2, 3])
    r  # r已经计算出来[1, 4, 9]
  

    for x in itertools.starmap(lambda x,y: x*y,zip([10, 20, 30],itertools.count(1))):
        print(x)# 10 40 90
   

    # 迭代实现“惰性计算”,在需要时才计算。
    r = itertools.starmap(lambda x: x * x, itertools.count(1))
    for n in itertools.takewhile(lambda x: x < 100, r):
        print(n)

    itertools.starmap(os.path.join,
                              [('/bin', 'python'), ('/usr', 'bin', 'java'),
                               ('/usr', 'bin', 'perl'), ('/usr', 'bin', 'ruby')])
            # =>/bin/python, /usr/bin/java, /usr/bin/perl, /usr/bin/ruby

itertools.accumulate

itertools.accumulate(iterable [,func ] )
根据条件(func2(x,y)=最大,累积乘)累计前n个元素
参数:
func:是两个参数的函数。输入迭代元素的类型是func参数类型
func设定值 min(),max(), operator.mul();
实例:

data = [3, 4, 6, 2, 1, 9, 0, 7, 5, 8]
list(accumulate(data, operator.mul)) # running product
# [3, 12, 72, 144, 144, 1296, 0, 0, 0, 0]
list(accumulate(data, max)) # running maximum
# [3, 4, 6, 6, 6, 9, 9, 9, 9, 9]

import itertools
a= [1, 2, 3, 4, 5]
a1=list(itertools.accumulate(a, lambda x, y: x*10 + y)) #12=y:1*10+x:2
print(a1)# [1, 12, 123, 1234, 12345]

from functools import reduce
a1=reduce(lambda x, y: x*10 + y,a) #12=y:1*10+x:2
print(a1,type(a1))#12345

func = lambda x, _: r + x
r = 2.0
x0 = 1.0
inputs = itertools.repeat(x0, 6) # only the initial value is used无休止或最多n次

a=[format(x, '.2f') for x in itertools.accumulate(inputs,func)]
print(a)
# ['1.00', '3.00', '5.00', '7.00', '9.00', '11.00']
2.3.选择元素
# 迭代器根据条件选择元素


filterfalse(predicate, iter)根据条件(func=false)选择元素
#predicate=false返回元素:是相反的filter()
# itertools.filterfalse(is_even, itertools.count()) =>1, 3, 5, 7, ...
# filterfalse(lambda x: x%2, range(10)) --> 0 2 4 6 8

itertools.takewhile(predicate, iter)根据条件(func=true)选择元素
# predicate=true返回元素。一旦条件=false,迭代器结束
# def less_than_6(x):
# return x < 6
# itertools.takewhile(less_than_6, itertools.count()) => 0, 1, 2, 3, 4, 5
# takewhile(lambda x: x<5, [1,4,6,4,1]) --> 1 4

g1 = itertools.count(1)
g2 = itertools.takewhile(lambda x: x <= 5, g1)
list(g2) # [1, 2, 3, 4]


itertools.dropwhile(predicate, iter)根据第一个(func=false)选择其后all元素
# 当predicate=true时创建一个从迭代器中删除元素的迭代器,之后,返回每个元素。
# 注意,只有predicate=false迭代器才会产生输出,因此可能会有较长的启动时间。
# dropwhile(lambda x: x<5, [1,4,6,4,1]) --> 6 4 1

itertools.compress(data, selectors)根据条件(boolData=Ture)返回对应data的元素
#需要两个迭代器:数据+选择,返回选择器元素为true 数据元素
# 任何一个元素用尽时停止:
# ompress([1,2,3,4,5], [True, True, False, False, True]) =>1, 2, 5
# compress('ABCDEF', [1,0,1,0,1,1]) --> A C E F

2.4.组合函数

itertools.combinations(iterable, r)r数量元素排列组合:ABCD-> AB AC AD BC BD CD
# 从输入迭代中返回元素的r长度子序列
# 每个元组内的元素保持与迭代返回它们相同的顺序
combinations('ABCD', 2) --> AB AC AD BC BD CD
combinations(range(4), 3) --> 012 013 023 123


itertools.combinations_with_replacement(iterable,r )r数量元素排列组合:ABCD-> AA AB AC BB BC CC
# 从输入迭代中返回元素的r长度子序列, 允许单个元素重复多次。
# 组合按字典顺序排列。所以,如果输入迭代被排序,组合元组将按排序顺序生成。
combinations_with_replacement('ABC', 2) --> AA AB AC BB BC CC
combinations_with_replacement([1, 2, 3], 2) =># (1,1),(1,2),(1,3),(2,2),(2,3),(3,3)


itertools.permutations(iterable, r=None)r数量元素排列组合:ABCD-> AB AC AD BA BC BD CA CB CD DA DB DC
# 返回所有可能的长度r的排列
#若无r,则相当于r=len(iterable)所有元素都被置换
# 排列以词典排序顺序排列。所以,如果输入迭代被排序,排列元组将按排序顺序生成。

itertools.permutations([1, 2, 3], 2) =># (1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2)
itertools.permutations('aba', 3) =>
('a', 'b', 'a'), ('a', 'a', 'b'), ('b', 'a', 'a'),
('b', 'a', 'a'), ('a', 'a', 'b'), ('a', 'b', 'a')

permutations('ABCD', 2) --> AB AC AD BA BC BD CA CB CD DA DB DC
permutations(range(3)) --> 012 021 102 120 201 210


itertools.product(* iterables,repeat = 1 )笛卡尔乘积'ABCD', 'xy'--> Ax Ay Bx By Cx Cy Dx Dy
输入迭代的笛卡尔乘积。
# 相当于生成器表达式((x,y) for x in A for y in B)
# 自身迭代:product(A,repeat=4)product(A, A, A, A)

product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy
product(range(2), repeat=3) --> 000 001 010 011 100 101 110 111

2.5.分组元素

itertools.groupby(iter,key_func=None)根据条件(func)将数据分组

用途:
迭代器中相邻重复元素挑出来放在一起:
# 计算迭代器返回的每个元素的键值。如果你没有提供关键功能,关键就是每个元素本身。
# 从基础迭代中收集具有相同键值的所有连续元素,并返回包含键值的2元组流和包含该键的元素的迭代器。
# 假设基础迭代器的内容将根据密钥进行排序。
# 挑选规则:
# 作用于函数两个元素返回值相等,这两个元素被认为是在一组,返回值作为组的key
# 注意:
# 返回的迭代器共享基础迭代器,在请求迭代器2及其相应的键之前,您必须保存使用iterator-1的结果

groups = []
uniquekeys = []
data = sorted(data, key=keyfunc)
    for k, g in groupby(data, keyfunc):
        groups.append(list(g))      # Store group iterator as a list
        uniquekeys.append(k)
    [k for k, g in groupby('AAAABBBCCDAABBB')] --> A B C D A B
    [list(g) for k, g in groupby('AAAABBBCCD')] --> AAAA BBB CC D
     


    实例:
    
    city_list = [('HeNan', 'A1'), ('HuNan', 'A1'), ('HeBei', 'A1'),
                 ('NanYan', 'A2'), ('HeBi', 'A2'),
                 ('XinYe', 'A3'), ('DengXian', 'A3'), ('NanZhao', 'A3')
                 ]#注意顺序

    def get_state(city_state):
        return city_state[1]
    g=itertools.groupby(city_list, get_state)
 

    for key, groups in g:
        group = [v for v, k in list(groups)]
        print(key, group)
        # A1['HeNan', 'HuNan', 'HeBei']
        # A2['NanYan', 'HeBi']
        # A3['XinYe', 'DengXian', 'NanZhao']
    

    for key, group in itertools.groupby('AAABBBCCAAA'):print(key,list(group))
    '''
    A['A', 'A', 'A']
    B['B', 'B', 'B']
    C['C', 'C']
    A['A', 'A', 'A']
    '''
    
    # 忽略大小写分组
    for key, group in itertools.groupby('AaaBBbcCAAa',lambda c:c.upper()):print(key,list(group))
    '''
    A['A', 'a', 'a']
    B['B', 'B', 'b']
    C['c', 'C']
    A['A', 'A', 'a']
    '''

猜你喜欢

转载自blog.csdn.net/tcy23456/article/details/84075938
今日推荐