Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # yield_combos.py
- def combos(iterable, r):
- iter = tuple(iterable)
- n = len(iter)
- if r > n:
- return
- indices = range(r)
- yield tuple(iter[i] for i in indices)
- while True:
- for i in reversed(range(r)):
- if indices[i] != i + n - r:
- break
- else:
- return
- indices[i] += 1
- for j in range(i+1, r):
- indices[j] = indices[j-1] + 1
- yield tuple(iter[i] for i in indices)
- z = combos('ABCD', 2) # --> AB AC AD BC BD CD
- while 1:
- try:
- print next(z)
- except:
- break
- for z in combos(range(4), 3): # --> 012 013 023 123
- print z
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement