itertools之其他常用功能函数

一、itertools.accumulate

语法:itertools.accumulate(iterable[, func])

注解:
Make an iterator that returns accumulated sums, or accumulated results of other binary functions (specified via the optional func argument). If func is supplied, it should be a function of two arguments. Elements of the input iterable may be any type that can be accepted as arguments to func. (For example, with the default operation of addition, elements may be any addable type including Decimal or Fraction.) If the input iterable is empty, the output iterable will also be empty.

个人理解:这个方法主要是提供了一个累计的功能,至于如何累计,我们可以根据后面的可选参数fuc传入一个函数的引用,通过这个函数来实现某种累计的方法。

举个例子:

def add_two(x, y):  
	return x + y  

li = [1, 3, 5, 7, 9]
print([i for i in itertools.accumulate(li, func=add_two)])  

# 输出结果为:  
[1, 4, 9, 16, 25]  

上述案例实现了一个简单的累加功能,注意了,在这里我们传入的func参数必须是要接收2个参数,每次累加,都是将上一次累加的结果作为第一个参数,迭代器中下一个元素作为第二个参数去做累计运算。其实我们还可以做一些其它的累计运算,比如说求最值,平均值等等:

li = [8, 25, 16, 6, 39, 22, 15]
print(i for i in itertools.accumulate(li, func=min))

# 输出结果为:  
[8, 8, 8, 6, 6, 6, 6]

二、itertools.chain

语法:itertools.chain(*iterables)

注解:
Make an iterator that returns elements from the first iterable until it is exhausted, then proceeds to the next iterable, until all of the iterables are exhausted. Used for treating consecutive sequences as a single sequence.

个人理解:这个方法的作用是将多个迭代器对象里的元素按照顺序组合成一个新的迭代器对象。

举个例子:

print([i for i in itertools.chain('ABC', 'DEF')])

# 输出结果为:  
['A', 'B', 'C', 'D', 'E', 'F']  

这里需要注意的是:新的迭代器对象所包含的元素,是由原始的多个迭代器对象遍历而得的单个元素组成。

三、itertools.chain.from_iterable

语法:itertools.chain.from_iterable(iterable)

注解:
Alternate constructor for chain(). Gets chained inputs from a single iterable argument that is evaluated lazily.

个人理解:实际上是chain的构造方法,我们只需要传入一个包含多个迭代器对象的容器即可。

举个例子:

print(i for i in itertools.chain.from_iterable(['ABC', 'DEF']))  

# 输出结果为:  
['A', 'B', 'C', 'D', 'E', 'F']  

print([i for i in itertools.chain.from_iterable({'A': 'B', 'C': 'D', 'E': 'F'})])

# 输出结果为:  
['A', 'C', 'E']  

四、itertools.compress

语法:itertools.compress(data, selectors)

注解:
Make an iterator that filters elements from data returning only those that have a corresponding element in selectors that evaluates to True. Stops when either the data or selectors iterables has been exhausted.

个人理解:这里我们可以实现这样一个操作,传入两个iterable作为compress的参数,其中selectors是过滤器,data是我们传入的原始待处理的数据,compress的结果就是dataselectors的元素一一对应,判断selectors的元素是否为True,是即保留对应data的元素,否则舍弃掉对应data的元素。

举个例子:

print(i for i in itertools.compress('ABCDEFG', 'You Need Python'))

# 结果输出为:  
['A', 'B', 'C', 'D', 'E', 'F', 'G']  

五、itertools.dropwhile

语法:itertools.dropwhile(predicate, iterable)

注解:
Make an iterator that drops elements from the iterable as long as the predicate is true; afterwards, returns every element. Note, the iterator does not produce any output until the predicate first becomes false, so it may have a lengthy start-up time.

个人理解:这个方法实现了这样一种操作,传入两个参数predicateiterablepredicate是函数的引用,dropwhileiterable的元素x依次传入到predicate函数里面,观察调用结果,直到predicate(x)的结果为False时,将此时的x以及iterable中剩下的元素返回。

举个例子:

print([i for i in i tertools.dropwhile(lambda x: x<6, [1, 4, 2, 6, 5, 3, 4, 2])])  

# 输出结果为:  
[6, 5, 3, 4, 2]  

六、itertools.filterfalse

语法:itertools.filterfalse(predicate, iterable)

注解:
Make an iterator that filters elements from iterable returning only those for which the predicate is False. If predicate is None, return the items that are false.

个人理解:这个方法实现了这样一种操作,我们给filterfalse提供两个参数分别是predicateiterablepredicate是我们需要传入的映射函数,iterable是我们传入的需要处理的数据(可迭代对象),这里filterfalse每次从iterable中遍历取出一个元素x然后调用predicate函数,如果predicate(x)返回的结果布尔值为False,则保留该元素x,否则舍弃。

举个例子:

print([i for i in itertools.filterfalse(lambda x: x%2, [0, 1, 2, 3, 4, 5])])  

# 输出结果为:  
[0, 2, 4]  

注意:这里如果传入的predicate是None,则最后的结果是直接判断iterable中每个元素的布尔值,留下其中为False的元素。

举个例子:

print([i for i in itertools.filterfalse(None, [0, 1, 2, 3, 4, 5])])  

# 输出结果为:  
[0]

七、itertools.groupby

语法:itertools.groupby(iterable, key=None)

注解:
Make an iterator that returns consecutive keys and groups from the iterable. The key is a function computing a key value for each element. If not specified or is None, key defaults to an identity function and returns the element unchanged. Generally, the iterable needs to already be sorted on the same key function.

个人理解:这个方法实现了这样一个操作,向groupby传入iterable可迭代对象,它会依次遍历iterable的每个元素,以该元素的值为key,该元素连续出现的这几次的值作为新的迭代对象作为value,直到遍历完iterable中的所有元素为止。

举个例子:

for k, v in itertools.groupby([6, 6, 2, 2, 2, 7, 3, 3, 3, 3, 9]):  
	print(k, list(v))  

# 输出结果为:  
6 [6, 6]  
2 [2, 2, 2]  
7 [7]  
3 [3, 3, 3, 3]  
9 [9]  

注意:我们还可以在groupby中定义key参数,可以定制化最后的结果key。例如:

for k, v in itertools.groupby([6, 6, 2, 2, 2, 7, 3, 3, 3, 3, 9], key=lambda x: x+1):  
	print(k, list(v))

# 输出结果为:  
7 [6, 6]  
3 [2, 2, 2]  
8 [7]  
4 [3, 3, 3, 3]  
10 [9]  

那么什么情况下我们能使用到这样的功能呢?
其实除了处理一般的算法问题,比如说统计字符串或者列表当中相邻元素相同的个数即元素值以外,在大数据中,我们会有统计词频的需求,这个时候我们即可将所有的关键词收集起来,最后使用groupby进行统计即可,非常的便捷,前提是需要将这些关键词进行排序。举个例子:

words = ['python', 'java', 'c++', 'java', 'python', 'python', 'php', 'python', 'java', 'python']
for k, v in itertools.groupby(sorted(words)):  
	print(k, list(v).count(k))

# 输出结果为:  
c++ 1
java 3
php 1
python 5  

八、itertools.islice

语法:
itertools.islice(iterable, stop)
itertools.islice(iterable, start, stop[, step])

注解:
Make an iterator that returns selected elements from the iterable. If start is non-zero, then elements from the iterable are skipped until start is reached. Afterward, elements are returned consecutively unless step is set higher than one which results in items being skipped. If stop is None, then iteration continues until the iterator is exhausted, if at all; otherwise, it stops at the specified position. Unlike regular slicing, islice() does not support negative values for start, stop, or step.

个人理解:这个方法实际上实现了对可迭代对象的切片操作,需要注意的是,islice中传入的参数startstopstep不能为负数

举个例子:

print([i for i in itertools.islice('ABCDEF', 2)])  

# 输出结果为:  
['A', 'B']  

print([i for i in itertools.islice('ABCDEF', 2, 4)])  

# 输出结果为:  
['C', 'D']  

print([i for i in itertools.islice('ABCDEFG', 2, None, 2)])  

# 输出结果为:  
['C', 'E', 'G']  

九、itertools.starmap

语法:itertools.starmap(function, iterable)

注解:
Make an iterator that computes the function using arguments obtained from the iterable. Used instead of map() when argument parameters are already grouped in tuples from a single iterable (the data has been “pre-zipped”). The difference between map() and starmap() parallels the distinction between function(a,b) and function(*c).

个人理解:这个方法实现了类似于map()的操作,但是,starmap()能处理需要多个参数的function的场景,而map()只能处理接收单个参数function的场景。

举个例子:

print([i for i in itertools.starmap(pow, [(2, 5), (5, 2), (10, 3)])])  

# 输出结果为:  
[32, 25, 1000]  

十、itertools.takewhile

语法:itertools.takewhile(predicate, iterable)

注解:
Make an iterator that returns elements from the iterable as long as the predicate is true.

个人理解:其实此方法的操作与前面的dropwhile类似,只不过在这里takewhile保留了iterable中调用predicate函数后结果的布尔值为True的元素,如果为False,则从此元素开始后面的元素都会被舍弃。所以iterable的第一个元素的布尔值如果为False, 不管后面的元素是多少,它的结果都为空。

举个例子:

print([i for i in itertools.takewhile(lambda x: x>3, [8, 6, 2, 9, 1, 10])])  

# 输出结果为:  
[8, 6]  

十一、itertools.tee

语法:itertools.tee(iterable, n=2)

注解:
Return n independent iterators from a single iterable.

个人理解:给这个方法传入一个iterable对象,她能生成n(默认为2)个相同且互相独立的iterable对象。

举个例子:

for tmp in [i for i in itertools.tee('ABCDEFG', 3)]:  
	print(list(tmp))  

# 输出结果为:  
['A', 'B', 'C', 'D', 'E', 'F', 'G']  
['A', 'B', 'C', 'D', 'E', 'F', 'G']  
['A', 'B', 'C', 'D', 'E', 'F', 'G']  

十二、itertools.zip_longest

语法:*itertools.zip_longest(iterables, fillvalue=None)

注解:
Make an iterator that aggregates elements from each of the iterables. If the iterables are of uneven length, missing values are filled-in with fillvalue. Iteration continues until the longest iterable is exhausted.

个人理解:此方法提供的操作类似于zip(),与zip()不同的是,zip_longest()是以所有iterable中长度最长的那个为基准进行zip()操作,遇到长度较短的iterable时,会以fillvalue(默认为None)指定的值去填充到相应的长度。

举个例子:

print([i for i in itertools.zip_longest('ab', 'cdefg', 'xy', fillvalue='*')])  

# 输出结果为:  
[('a', 'c', 'x'),
 ('b', 'd', 'y'),
 ('*', 'e', 'z'),
 ('*', 'f', '*'),
 ('*', 'g', '*')]  

print([i for i in itertools.zip_longest('ab', 'cdefg', 'xy')])  

# 输出结果为:  
[('a', 'c', 'x'),
 ('b', 'd', 'y'),
 (None, 'e', 'z'),
 (None, 'f', None),
 (None, 'g', None)]  

总结

如果需要更深入的了解,请移步官方文档:
https://docs.python.org/3.6/library/itertools.html

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值