📜  Python中的迭代器函数 |设置 2 (islice(), starmap(), tee()..)

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

Python中的迭代器函数 |设置 2 (islice(), starmap(), tee()..)

Python中的迭代器函数 |设置 1

1. islice(iterable, start, stop, step) :- 这个迭代器有选择地打印在其作为参数传递的可迭代容器中提到的值。这个迭代器有4 个参数,可迭代容器、起始位置、结束位置和步长。

2. starmap(func., tuple list) :- 这个迭代器接受一个函数和元组列表作为参数,并根据函数从列表的每个元组返回值

# Python code to demonstrate the working of 
# islice() and starmap()
  
# importing "itertools" for iterator operations
import itertools
  
# initializing list 
li = [2, 4, 5, 7, 8, 10, 20]
  
# initializing tuple list
li1 = [ (1, 10, 5), (8, 4, 1), (5, 4, 9), (11, 10 , 1) ]
  
  
# 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)))
  
# 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,li1)))

输出:

The sliced list values are : [4, 7, 10]
The values acc. to function are : [1, 1, 4, 1]

3. takewhile(func, iterable) :- 这个迭代器与 dropwhile() 相反,它打印值直到函数第一次返回 false

4. tee(iterator, count) :- 这个迭代器将容器拆分为参数中提到的多个迭代器

# Python code to demonstrate the working of 
# takewhile() and tee()
  
# importing "itertools" for iterator operations
import itertools
  
# initializing list 
li = [2, 4, 6, 7, 8, 10, 20]
  
# storing list in iterator
iti = iter(li)
  
# 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 )))
  
# 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 list values till 1st false value are : [2, 4, 6]
The iterators are : 
[2, 4, 6, 7, 8, 10, 20]
[2, 4, 6, 7, 8, 10, 20]
[2, 4, 6, 7, 8, 10, 20]

5. zip_longest(iterable1, iterable2, fillval.) :- 此迭代器按顺序交替打印可迭代对象的值。如果其中一个可迭代对象被完全打印,剩余的值将由分配给 fillvalue 的值填充

# Python code to demonstrate the working of 
# zip_longest()
  
# importing "itertools" for iterator operations
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', '_')

组合迭代器

1. product(iter1, iter2) :- 这个迭代器打印作为参数传递的两个可迭代容器的笛卡尔积

2. permutations(iter, group_size) :- 这个迭代器打印可迭代的所有元素的所有可能排列。每个置换组的大小由 group_size 参数决定

# Python code to demonstrate the working of 
# product() and permutations()
  
# importing "itertools" for iterator operations
import itertools
  
# using product() to print the cartesian product
print ("The cartesian product of the containers is : ")
print (list(itertools.product('AB','12')))
  
# using permutations to compute all possible permutations
print ("All the permutations of the given container is : ")
print (list(itertools.permutations('GfG',2)))

输出:

The cartesian product of the containers is : 
[('A', '1'), ('A', '2'), ('B', '1'), ('B', '2')]
All the permutations of the given container is : 
[('G', 'f'), ('G', 'G'), ('f', 'G'), ('f', 'G'), ('G', 'G'), ('G', 'f')]

3.combinations(iterable, group_size) :- 这个迭代器按指定的组大小排序顺序打印传入参数的容器的所有可能组合(不替换)

4.combinations_with_replacement(iterable, group_size) :- 这个迭代器按指定的组大小排序顺序打印传入参数的容器的所有可能组合(带替换)

# Python code to demonstrate the working of 
# combination() and combination_with_replacement()
  
# importing "itertools" for iterator operations
import itertools
  
# using combinations() to print every combination
# (without replacement)
print ("All the combination of container in sorted order(without replacement) is : ")
print (list(itertools.combinations('1234',2)))
  
# using combinations_with_replacement() to print every combination
# with replacement
print ("All the combination of container in sorted order(with replacement) is : ")
print (list(itertools.combinations_with_replacement('GfG',2)))

输出:

All the combination of container in sorted order(without replacement) is : 
[('1', '2'), ('1', '3'), ('1', '4'), ('2', '3'), ('2', '4'), ('3', '4')]
All the combination of container in sorted order(with replacement) is : 
[('G', 'G'), ('G', 'f'), ('G', 'G'), ('f', 'f'), ('f', 'G'), ('G', 'G')]

无限迭代器

1. count(start, step) :- 这个迭代器从“start”数字开始打印并无限打印。如果提到步骤,则跳过数字,否则步骤默认为 1。

例子 :

iterator.count(5,2) prints -- 5,7,9,11...infinitely

2. cycle(iterable) :- 这个迭代器从传递的容器中按顺序打印所有值。当所有元素都以循环方式打印时,它会重新从头开始打印

例子 :

iterator.cycle([1,2,3,4]) prints -- 1,2,3,4,1,2,3,4,1...infinitely

3. repeat(val, num) :- 这个迭代器无限次地重复打印传递的值。如果编号。提到,他们直到那个数字。

# 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]