📜  Python迭代工具

📅  最后修改于: 2022-05-13 01:55:24.200000             🧑  作者: Mango

Python迭代工具

Python 的 Itertool 是一个模块,它提供了在迭代器上工作以生成复杂迭代器的各种函数。该模块作为一种快速、高效的内存工具,可单独使用或组合使用以形成迭代器代数
例如,假设有两个列表,您想将它们的元素相乘。有几种方法可以实现这一点。可以使用简单的方法,即同时迭代两个列表的元素并将它们相乘。另一种方法是使用 map函数,即将 mul运算符作为第一个参数传递给 map函数,将 Lists 作为第二个和第三个参数传递给这个函数。让我们看看每种方法所花费的时间。

Python3
# Python program to demonstrate
# iterator module
 
 
import operator
import time
 
# Defining lists
L1 = [1, 2, 3]
L2 = [2, 3, 4]
 
# Starting time before map
# function
t1 = time.time()
 
# Calculating result
a, b, c = map(operator.mul, L1, L2)
 
# Ending time after map
# function
t2 = time.time()
 
# Time taken by map function
print("Result:", a, b, c)
print("Time taken by map function: %.6f" %(t2 - t1))
 
# Starting time before naive
# method
t1 = time.time()
 
# Calculating result using for loop
print("Result:", end = " ")
for i in range(3):
    print(L1[i] * L2[i], end = " ")
     
# Ending time after naive
# method
t2 = time.time()
print("\nTime taken by for loop: %.6f" %(t2 - t1))


Python3
# Python program to demonstrate
# infinite iterators
   
import itertools
   
# for in loop
for i in itertools.count(5, 5):
    if i == 35:
        break
    else:
        print(i, end =" ")


Python3
# Python program to demonstrate
# infinite iterators
   
import itertools
   
count = 0
   
# for in loop
for i in itertools.cycle('AB'):
    if count > 7:
        break
    else:
        print(i, end = " ")
        count += 1


Python3
# Python program to demonstrate
# infinite iterators
   
import itertools
   
l = ['Geeks', 'for', 'Geeks']
   
# defining iterator
iterators = itertools.cycle(l)
   
# for in loop
for i in range(6):
       
    # Using next function
    print(next(iterators), end = " ")


Python3
# Python code to demonstrate the working of  
# repeat() 
     
# importing "itertools" for iterator operations 
import itertools 
     
# using repeat() to repeatedly print number 
print ("Printing the numbers repeatedly : ") 
print (list(itertools.repeat(25, 4)))


Python3
# import the product function from itertools module
from itertools import product
   
print("The cartesian product using repeat:")
print(list(product([1, 2], repeat = 2)))
print()
   
print("The cartesian product of the containers:")
print(list(product(['geeks', 'for', 'geeks'], '2')))
print()
   
print("The cartesian product of the containers:")
print(list(product('AB', [3, 4])))


Python3
# import the product function from itertools module
from itertools import permutations
   
print ("All the permutations of the given list is:") 
print (list(permutations([1, 'geeks'], 2)))
print()
  Terminating iterators
print ("All the permutations of the given string is:") 
print (list(permutations('AB')))
print()
   
print ("All the permutations of the given container is:") 
print(list(permutations(range(3), 2)))


Python3
# import combinations from itertools module
   
from itertools import combinations
   
print ("All the combination of list in sorted order(without replacement) is:") 
print(list(combinations(['A', 2], 2)))
print()
   
print ("All the combination of string in sorted order(without replacement) is:")
print(list(combinations('AB', 2)))
print()
   
print ("All the combination of list in sorted order(without replacement) is:")
print(list(combinations(range(2), 1)))


Python3
# import combinations from itertools module
   
from itertools import combinations_with_replacement
   
print ("All the combination of string in sorted order(with replacement) is:")
print(list(combinations_with_replacement("AB", 2)))
print()
   
print ("All the combination of list in sorted order(with replacement) is:")
print(list(combinations_with_replacement([1, 2], 2)))
print()
   
print ("All the combination of container in sorted order(with replacement) is:")
print(list(combinations_with_replacement(range(2), 1)))


Python3
# Python code to demonstrate the working of 
# accumulate()
 
 
import itertools
import operator
 
# initializing list 1
li1 = [1, 4, 5, 7]
   
# using accumulate()
# prints the successive summation of elements
print ("The sum after each iteration is : ", end ="")
print (list(itertools.accumulate(li1)))
   
# using accumulate()
# prints the successive multiplication of elements
print ("The product after each iteration is : ", end ="")
print (list(itertools.accumulate(li1, operator.mul)))
   
# using accumulate()
# prints the successive summation of elements
print ("The sum after each iteration is : ", end ="")
print (list(itertools.accumulate(li1)))
   
# using accumulate()
# prints the successive multiplication of elements
print ("The product after each iteration is : ", end ="")
print (list(itertools.accumulate(li1, operator.mul)))


Python3
# Python code to demonstrate the working of 
# and chain()
   
 
import itertools
 
# initializing list 1
li1 = [1, 4, 5, 7]
   
# initializing list 2
li2 = [1, 6, 5, 9]
   
# initializing list 3
li3 = [8, 10, 5, 4]
 
# using chain() to print all elements of lists
print ("All values in mentioned chain are : ", end ="")
print (list(itertools.chain(li1, li2, li3)))


Python3
# Python code to demonstrate the working of 
# chain.from_iterable()
 
 
import itertools
 
 
# initializing list 1
li1 = [1, 4, 5, 7]
   
# initializing list 2
li2 = [1, 6, 5, 9]
   
# initializing list 3
li3 = [8, 10, 5, 4]
   
# initializing list of list
li4 = [li1, li2, li3]
 
# using chain.from_iterable() to print all elements of lists
print ("All values in mentioned chain are : ", end ="")
print (list(itertools.chain.from_iterable(li4)))


Python3
# Python code to demonstrate the working of 
# and compress()
 
 
import itertools
 
 
# using compress() selectively print data values
print ("The compressed values in string are : ", end ="")
print (list(itertools.compress('GEEKSFORGEEKS', [1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0])))


Python3
# Python code to demonstrate the working of 
# dropwhile()
 
 
import itertools
 
 
# initializing list 
li = [2, 4, 5, 7, 8]
   
# using dropwhile() to start displaying after condition is false
print ("The values after condition returns false : ", end ="")
print (list(itertools.dropwhile(lambda x : x % 2 == 0, li)))


Python3
# Python code to demonstrate the working of 
# filterfalse()
   
 
import itertools
   
# initializing list 
li = [2, 4, 5, 7, 8]
 
# using filterfalse() to print false values
print ("The values that return false to function are : ", end ="")
print (list(itertools.filterfalse(lambda x : x % 2 == 0, li)))


Python3
# Python code to demonstrate the working of 
# islice()
   
  
import itertools
   
# initializing list 
li = [2, 4, 5, 7, 8, 10, 20]
     
# using islice() to slice the list acc. to need
# starts printing from 2nd index till 6th skipping 2
print ("The sliced list values are : ", end ="")
print (list(itertools.islice(li, 1, 6, 2)))


Python3
# Python code to demonstrate the working of 
# starmap()
   
 
import itertools
   
   
# initializing tuple list
li = [ (1, 10, 5), (8, 4, 1), (5, 4, 9), (11, 10, 1) ]
   
# using starmap() for selection value acc. to function
# selects min of all tuple values
print ("The values acc. to function are : ", end ="")
print (list(itertools.starmap(min, li)))


Python3
# Python code to demonstrate the working of 
# takewhile()
   
 
import itertools
   
# initializing list 
li = [2, 4, 6, 7, 8, 10, 20]
   
# using takewhile() to print values till condition is false.
print ("The list values till 1st false value are : ", end ="")
print (list(itertools.takewhile(lambda x : x % 2 == 0, li )))


Python3
# Python code to demonstrate the working of 
# tee()
   
 
import itertools
   
# initializing list 
li = [2, 4, 6, 7, 8, 10, 20]
   
# storing list in iterator
iti = iter(li) 
   
# using tee() to make a list of iterators
# makes list of 3 iterators having same values.
it = itertools.tee(iti, 3)
   
# printing the values of iterators
print ("The iterators are : ")
for i in range (0, 3):
    print (list(it[i]))


Python3
# Python code to demonstrate the working of 
# zip_longest()
   
 
import itertools
   
# using zip_longest() to combine two iterables.
print ("The combined values of iterables is  : ")
print (*(itertools.zip_longest('GesoGes', 'ekfrek', fillvalue ='_' )))


输出:

Result: 2 6 12
Time taken by map function: 0.000005
Result: 2 6 12 
Time taken by for loop: 0.000014

在上面的例子中,可以看出map函数所用的时间大约是for循环所用时间的一半。这表明 itertools 是快速、节省内存的工具。

该模块提供的不同类型的迭代器是:

  • 无限迭代器
  • 组合迭代器
  • 终止迭代器

无限迭代器

Python中的迭代器是可以与“for in loop”一起使用的任何Python类型。 Python列表、元组、字典和集合都是内置迭代器的示例。但是迭代器对象不必耗尽,有时它可以是无限的。这种类型的迭代器被称为无限迭代器

Python提供了三种类型的无限迭代器:

  • count(start, step):此迭代器从“start”数字开始打印并无限打印。如果提到步骤,则跳过数字,否则步骤默认为 1。请参阅下面的示例,了解其与 for in 循环的使用。
    例子:

Python3

# Python program to demonstrate
# infinite iterators
   
import itertools
   
# for in loop
for i in itertools.count(5, 5):
    if i == 35:
        break
    else:
        print(i, end =" ")

输出:

5 10 15 20 25 30
  • cycle(iterable):此迭代器从传递的容器中按顺序打印所有值。当所有元素都以循环方式打印时,它会再次从头开始打印
    示例 1:

Python3

# Python program to demonstrate
# infinite iterators
   
import itertools
   
count = 0
   
# for in loop
for i in itertools.cycle('AB'):
    if count > 7:
        break
    else:
        print(i, end = " ")
        count += 1

输出:

A B A B A B A B 

示例 2:使用下一个函数。

Python3

# Python program to demonstrate
# infinite iterators
   
import itertools
   
l = ['Geeks', 'for', 'Geeks']
   
# defining iterator
iterators = itertools.cycle(l)
   
# for in loop
for i in range(6):
       
    # Using next function
    print(next(iterators), end = " ")

组合迭代器
输出:

Geeks for Geeks Geeks for Geeks 
  • repeat(val, num):这个迭代器无限次地重复打印传递的值。如果提到了可选关键字 num,那么它会重复打印 num 次。
    例子:

Python3

# Python code to demonstrate the working of  
# repeat() 
     
# importing "itertools" for iterator operations 
import itertools 
     
# using repeat() to repeatedly print number 
print ("Printing the numbers repeatedly : ") 
print (list(itertools.repeat(25, 4)))

输出:

Printing the numbers repeatedly : 
[25, 25, 25, 25]

组合迭代器

用于简化组合构造(例如排列、组合和笛卡尔积)的递归生成器称为组合迭代器。
在Python中有 4 个组合迭代器:

  • Product():这个工具计算输入迭代的笛卡尔积。为了计算一个可迭代对象与自身的乘积,我们使用可选的 repeat 关键字参数来指定重复次数。此函数的输出是按排序顺序的元组。
    例子:

Python3

# import the product function from itertools module
from itertools import product
   
print("The cartesian product using repeat:")
print(list(product([1, 2], repeat = 2)))
print()
   
print("The cartesian product of the containers:")
print(list(product(['geeks', 'for', 'geeks'], '2')))
print()
   
print("The cartesian product of the containers:")
print(list(product('AB', [3, 4])))

输出:

The cartesian product using repeat:
[(1, 1), (1, 2), (2, 1), (2, 2)]

The cartesian product of the containers:
[('geeks', '2'), ('for', '2'), ('geeks', '2')]

The cartesian product of the containers:
[('A', 3), ('A', 4), ('B', 3), ('B', 4)]
  • Permutations(): Permutations() 顾名思义,用于生成 iterable 的所有可能排列。所有元素都根据它们的位置而不是它们的值被视为唯一的。此函数采用可迭代对象和 group_size,如果未指定 group_size 的值或等于 None,则 group_size 的值成为可迭代对象的长度。
    例子:

Python3

# import the product function from itertools module
from itertools import permutations
   
print ("All the permutations of the given list is:") 
print (list(permutations([1, 'geeks'], 2)))
print()
  Terminating iterators
print ("All the permutations of the given string is:") 
print (list(permutations('AB')))
print()
   
print ("All the permutations of the given container is:") 
print(list(permutations(range(3), 2)))

输出:

All the permutations of the given list is:
[(1, 'geeks'), ('geeks', 1)]

All the permutations of the given string is:
[('A', 'B'), ('B', 'A')]

All the permutations of the given container is:
[(0, 1), (0, 2), (1, 0), (1, 2), (2, 0), (2, 1)]
  • Combinations():此迭代器以排序顺序打印指定组大小中传入参数的容器的所有可能组合(无需替换)
    例子:

Python3

# import combinations from itertools module
   
from itertools import combinations
   
print ("All the combination of list in sorted order(without replacement) is:") 
print(list(combinations(['A', 2], 2)))
print()
   
print ("All the combination of string in sorted order(without replacement) is:")
print(list(combinations('AB', 2)))
print()
   
print ("All the combination of list in sorted order(without replacement) is:")
print(list(combinations(range(2), 1)))

输出:

All the combination of list in sorted order(without replacement) is:
[('A', 2)]

All the combination of string in sorted order(without replacement) is:
[('A', 'B')]

All the combination of list in sorted order(without replacement) is:
[(0, ), (1, )]
  • Combinations_with_replacement():此函数从可迭代的元素中返回长度为 n 的子序列,其中 n 是函数用于确定函数生成的子序列长度的函数。单个元素可以在 combination_with_replacement函数中重复。
    例子:

Python3

# import combinations from itertools module
   
from itertools import combinations_with_replacement
   
print ("All the combination of string in sorted order(with replacement) is:")
print(list(combinations_with_replacement("AB", 2)))
print()
   
print ("All the combination of list in sorted order(with replacement) is:")
print(list(combinations_with_replacement([1, 2], 2)))
print()
   
print ("All the combination of container in sorted order(with replacement) is:")
print(list(combinations_with_replacement(range(2), 1)))

输出:

All the combination of string in sorted order(with replacement) is:
[('A', 'A'), ('A', 'B'), ('B', 'B')]

All the combination of list in sorted order(with replacement) is:
[(1, 1), (1, 2), (2, 2)]

All the combination of container in sorted order(with replacement) is:
[(0, ), (1, )]Terminating iterators

终止迭代器

终止迭代器用于处理短输入序列并根据所用方法的功能生成输出。

不同类型的终止迭代器是:

  • 累积(iter,func):此迭代器有两个参数,可迭代目标和目标中值的每次迭代时将遵循的函数。如果未传递任何函数,则默认情况下会进行添加。如果输入迭代为空,则输出迭代也将为空。
    例子:

Python3

# Python code to demonstrate the working of 
# accumulate()
 
 
import itertools
import operator
 
# initializing list 1
li1 = [1, 4, 5, 7]
   
# using accumulate()
# prints the successive summation of elements
print ("The sum after each iteration is : ", end ="")
print (list(itertools.accumulate(li1)))
   
# using accumulate()
# prints the successive multiplication of elements
print ("The product after each iteration is : ", end ="")
print (list(itertools.accumulate(li1, operator.mul)))
   
# using accumulate()
# prints the successive summation of elements
print ("The sum after each iteration is : ", end ="")
print (list(itertools.accumulate(li1)))
   
# using accumulate()
# prints the successive multiplication of elements
print ("The product after each iteration is : ", end ="")
print (list(itertools.accumulate(li1, operator.mul)))

输出:

The sum after each iteration is : [1, 5, 10, 17]
The product after each iteration is : [1, 4, 20, 140]
The sum after each iteration is : [1, 5, 10, 17]
The product after each iteration is : [1, 4, 20, 140]
  • chain(iter1, iter2..):该函数用于逐个打印参数中提到的可迭代目标中的所有值。
    例子:

Python3

# Python code to demonstrate the working of 
# and chain()
   
 
import itertools
 
# initializing list 1
li1 = [1, 4, 5, 7]
   
# initializing list 2
li2 = [1, 6, 5, 9]
   
# initializing list 3
li3 = [8, 10, 5, 4]
 
# using chain() to print all elements of lists
print ("All values in mentioned chain are : ", end ="")
print (list(itertools.chain(li1, li2, li3)))

输出:

All values in mentioned chain are : [1, 4, 5, 7, 1, 6, 5, 9, 8, 10, 5, 4]
  • chain.from_iterable():这个函数的实现方式与 chain() 类似,但这里的参数是列表列表或任何其他可迭代容器。
    例子:

Python3

# Python code to demonstrate the working of 
# chain.from_iterable()
 
 
import itertools
 
 
# initializing list 1
li1 = [1, 4, 5, 7]
   
# initializing list 2
li2 = [1, 6, 5, 9]
   
# initializing list 3
li3 = [8, 10, 5, 4]
   
# initializing list of list
li4 = [li1, li2, li3]
 
# using chain.from_iterable() to print all elements of lists
print ("All values in mentioned chain are : ", end ="")
print (list(itertools.chain.from_iterable(li4)))

输出:

All values in mentioned chain are : [1, 4, 5, 7, 1, 6, 5, 9, 8, 10, 5, 4]
  • compress(iter, selector):此迭代器根据作为其他参数传递的布尔列表值选择性地从传递的容器中选择要打印的值。与 boolean true 相对应的参数被打印,否则全部被跳过。
    例子:

Python3

# Python code to demonstrate the working of 
# and compress()
 
 
import itertools
 
 
# using compress() selectively print data values
print ("The compressed values in string are : ", end ="")
print (list(itertools.compress('GEEKSFORGEEKS', [1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0])))

输出:

The compressed values in string are : ['G', 'F', 'G']
  • dropwhile(func, seq):此迭代器仅在 func 之后开始打印字符。 in 参数第一次返回 false。
    例子:

Python3

# Python code to demonstrate the working of 
# dropwhile()
 
 
import itertools
 
 
# initializing list 
li = [2, 4, 5, 7, 8]
   
# using dropwhile() to start displaying after condition is false
print ("The values after condition returns false : ", end ="")
print (list(itertools.dropwhile(lambda x : x % 2 == 0, li)))

输出:

The values after condition returns false : [5, 7, 8]
  • filterfalse(func, seq):顾名思义,这个迭代器只打印为传递的函数返回 false 的值。
    例子:

Python3

# Python code to demonstrate the working of 
# filterfalse()
   
 
import itertools
   
# initializing list 
li = [2, 4, 5, 7, 8]
 
# using filterfalse() to print false values
print ("The values that return false to function are : ", end ="")
print (list(itertools.filterfalse(lambda x : x % 2 == 0, li)))

输出:

The values that return false to function are : [5, 7]
  • islice(iterable, start, stop, step):此迭代器有选择地打印在其作为参数传递的可迭代容器中提到的值。这个迭代器有 4 个参数,可迭代容器、起始位置、结束位置和步长。
    例子:

Python3

# Python code to demonstrate the working of 
# islice()
   
  
import itertools
   
# initializing list 
li = [2, 4, 5, 7, 8, 10, 20]
     
# using islice() to slice the list acc. to need
# starts printing from 2nd index till 6th skipping 2
print ("The sliced list values are : ", end ="")
print (list(itertools.islice(li, 1, 6, 2)))

输出:

The sliced list values are : [4, 7, 10]
  • starmap(func., tuple list):这个迭代器接受一个函数和一个元组列表作为参数,并根据列表中每个元组的函数返回值。
    例子:

Python3

# Python code to demonstrate the working of 
# starmap()
   
 
import itertools
   
   
# initializing tuple list
li = [ (1, 10, 5), (8, 4, 1), (5, 4, 9), (11, 10, 1) ]
   
# using starmap() for selection value acc. to function
# selects min of all tuple values
print ("The values acc. to function are : ", end ="")
print (list(itertools.starmap(min, li)))

输出:

The values acc. to function are : [1, 1, 4, 1]
  • takewhile(func, iterable):这个迭代器与dropwhile()相反,它打印值直到函数第一次返回false。
    例子:

Python3

# Python code to demonstrate the working of 
# takewhile()
   
 
import itertools
   
# initializing list 
li = [2, 4, 6, 7, 8, 10, 20]
   
# using takewhile() to print values till condition is false.
print ("The list values till 1st false value are : ", end ="")
print (list(itertools.takewhile(lambda x : x % 2 == 0, li )))

输出:

The list values till 1st false value are : [2, 4, 6]
  • tee(iterator, count):-这个迭代器将容器拆分为参数中提到的多个迭代器。
    例子:

Python3

# Python code to demonstrate the working of 
# tee()
   
 
import itertools
   
# initializing list 
li = [2, 4, 6, 7, 8, 10, 20]
   
# storing list in iterator
iti = iter(li) 
   
# using tee() to make a list of iterators
# makes list of 3 iterators having same values.
it = itertools.tee(iti, 3)
   
# printing the values of iterators
print ("The iterators are : ")
for i in range (0, 3):
    print (list(it[i]))

输出:

The iterators are : 
[2, 4, 6, 7, 8, 10, 20]
[2, 4, 6, 7, 8, 10, 20]
[2, 4, 6, 7, 8, 10, 20]
  • zip_longest(iterable1, iterable2, fillval):此迭代器按顺序交替打印可迭代对象的值。如果其中一个可迭代对象被完全打印,则剩余的值将由分配给 fillvalue 的值填充。
    例子:

Python3

# Python code to demonstrate the working of 
# zip_longest()
   
 
import itertools
   
# using zip_longest() to combine two iterables.
print ("The combined values of iterables is  : ")
print (*(itertools.zip_longest('GesoGes', 'ekfrek', fillvalue ='_' )))

输出:

The combined values of iterables is  : 
('G', 'e') ('e', 'k') ('s', 'f') ('o', 'r') ('G', 'e') ('e', 'k') ('s', '_')