실제로 itertools 라이브러리와 생성기 기능을 사용하여이를 수행하는 매우 까다로운 방법이 있습니다.
from itertools import product, imap
def stringdigit(num_digits=10, start = None):
"""A generator function which returns string versions of a large iterated number with
leading zeros. start allows you to define a place to begin the iteration"""
treatfun = lambda x: ''.join(x)
for n in imap(treatfun, product('', repeat = num_digits)):
if start == None or n > start:
yield n
이렇게하면 필요한 "제로 패딩 된 문자열 형식"을 반환하는 반복기가 만들어집니다. iterable에서 반복 된 조합을 "정렬 된 순서"로 반복적으로 반환하는 제품 기능을 사용하여 작동합니다. num_digits 인수는 반환 할 총 자릿수를 지정합니다. start
은 반복을 시작할 위치를 지정합니다 (예 : 1111111에서 시작하려는 경우).
product
은 Python 2.6 릴리스와 함께 제공됩니다. 어떤 이유로 든 그 전에 무엇인가를 사용한다면 이것을 제품 정의로 사용하십시오. 문서 here에서 가져 왔습니다.
def product(*args, **kwds):
# product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy
# product(range(2), repeat=3) --> 000 001 010 011 100 101 110 111
pools = map(tuple, args) * kwds.get('repeat', 1)
result = [[]]
for pool in pools:
result = [x+[y] for x in result for y in pool]
for prod in result:
yield tuple(prod)
당신은 interator로에 대한 루프에서이 기능을 사용할 수 있습니다
for num in stringdigit(num_digits = 7):
#do stuff with num
희망을. -Will
확실히 "% 012d"% (22,)는 '000000000022'로 평가됩니다. –
그리고 트래비스 브래드쇼 (Travis Bradshaw). BTW, 나는 ID가있는 보이즈 (Travis Bradshaw)와 함께 고등학교에 다녔습니다. – orokusaki
itertools를 사용하지 않고 그 모든 타이핑을 저장하지 않는 이유는 무엇입니까? – JudoWill