이 기사에서는 Python 3.x에서 사용할 수 있는 네 가지 반복자 함수에 대해 알아봅니다. 또는 이전에는 누적(), 체인(), 필터 false(), dropwhile() 메서드가 있습니다.
이제 각각에 대해 자세히 살펴보겠습니다 -
Accumulate() &chain() 메소드
Accumulate() 메서드는 두 개의 인수를 사용합니다. 하나는 반복 가능한 작업이고 다른 하나는 수행할 함수/작업입니다. 기본적으로 두 번째 인수는 더하기 연산을 수행합니다.
Chain() 메서드는 모든 반복 가능한 대상을 연결한 후 모든 반복 가능한 대상을 인쇄합니다.
아래 예는 구현을 설명합니다 -
예시
import itertools
import operator as op
# initializing list 1
li1 = ['t','u','t','o','r']
# initializing list 2
li2 = [1,1,1,1,1]
# initializing list 3
li3 = ['i','a','l','s','p','o','i','n','t']
# using accumulate() add method
print ("The sum after each iteration is : ",end="")
print (list(itertools.accumulate(li1,op.add)))
# using accumulate() multiply method
print ("The product after each iteration is : ",end="")
print (list(itertools.accumulate(li2,op.mul)))
# using chain() method
print ("All values in mentioned chain are : ",end="")
print (list(itertools.chain(li1,li3))) 출력
The sum after each iteration is : ['t', 'tu', 'tut', 'tuto', 'tutor'] The product after each iteration is : [1, 1, 1, 1, 1] All values in mentioned chain are : ['t', 'u', 't', 'o', 'r', 'i', 'a', 'l', 's', 'p', 'o', 'i', 'n', 't']
Dropwhile() 및 filterfalse() 메서드
Drop while() 메서드는 조건을 확인하는 함수와 작동할 반복 가능한 입력을 받습니다. 조건이 false가 된 후 iterable의 모든 값을 반환합니다.
Filterfalse() 메서드는 조건을 확인하는 함수와 작동할 반복 가능한 입력을 받습니다. 주어진 조건이 거짓일 때 값을 반환합니다.
예시
import itertools
# list
l = ['t','u','t','o','r']
# using dropwhile() method
print ("The values after condition fails : ",end="")
print (list(itertools.dropwhile(lambda x : x!='o',l)))
# using filterfalse() method
print ("The values when condition fails : ",end="")
print (list(itertools.filterfalse(lambda x : x!='o',l))) 출력
The values after condition fails : ['o', 'r'] The values when condition fails : ['o']
결론
이 기사에서는 Python 3.x에서 사용할 수 있는 다양한 유형의 반복기 함수에 대해 배웠습니다. 또는 그 이전.