1
소스 목록을 반복하여 목록을 작성하고 있습니다. 특정 값을 발견하면 목록에서 다음 항목을 건너 뜁니다.for 루프에서 목록 항목을 건너 뛰십시오.
아래 process_things 함수를 작성하는 더 좋은 방법이 있습니까?
def test_skip(thing):
return thing == 'b'
def num_to_skip(thing):
return 3
def process_things(things):
"""
build list from things where we skip every test_skip() and the next num_to_skip() items
"""
result = []
skip_count = 0
for thing in things:
if test_skip(thing):
skip_count = num_to_skip(thing)
if skip_count > 0:
skip_count -= 1
continue
result.append(thing)
return result
source = list('abzzazyabyyab')
intended_result = list('aazyaa')
assert process_things(source) == intended_result