| 73 | |
| 74 | |
| 75 | def zip_longest(*iterables): |
| 76 | # zip_longest('ABCD', 'xy', '123') → Ax1 By2 Cy3 Dy3 |
| 77 | |
| 78 | iterators = list(map(iter, iterables)) |
| 79 | num_active = len(iterators) |
| 80 | if not num_active: |
| 81 | return |
| 82 | |
| 83 | while True: |
| 84 | values = [] |
| 85 | last_values = [None] * num_active |
| 86 | for i, iterator in enumerate(iterators): |
| 87 | try: |
| 88 | value = next(iterator) |
| 89 | except StopIteration: |
| 90 | num_active -= 1 |
| 91 | if not num_active: |
| 92 | return |
| 93 | value = last_values[i] |
| 94 | iterators[i] = itertools.repeat(value) |
| 95 | values.append(value) |
| 96 | last_values = values |
| 97 | yield tuple(values) |
| 98 | |
| 99 | |
| 100 | def run(args): |