Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- import copy
- def allCombos(scratch, data):
- '''
- Returns all possible combinations of objects in an array
- Args:
- ----
- scratch - an empty array
- data - the array to choose from
- Returns:
- ----
- None
- Yields:
- ------
- A generator function for returning all combinations.
- '''
- for j in range(len(data)):
- new_scratch = copy.copy(scratch)
- new_data = copy.copy(data)
- new_scratch.append(data[j])
- new_data = data[j+1:]
- yield new_scratch
- yield from allCombos(new_scratch,new_data) #recursive call woah boy
- scratch = []
- data = [1,2,3,4,5]
- for combo in allCombos(scratch,data):
- print(combo)
- comboList = [x for x in allCombos(scratch, data)]
- print(comboList)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement