I have a list of people and I want to make all combinations of their teams (of 3) with each other.
list_of_ppl = ['A','B','C','D','E','F']
What i need is:
>>> [['A','B','C'],['D','E','F']]
>>> [['A','B','D'],['C','E','F']]
>>> [['A','B','E'],['C','D','F']]
>>> [['A','B','F'],['C','D','E']]
>>> [['A','C','E'],['B','D','F']]
>>> and so on...
Edit: The len(list_of_ppl) can be greater than 6 and can be a number that is not divisible by 3. I only want teams of 3
For eg:
list_of_ppl = ['A','B','C','D','E','F','G']
>>> [['A','B','C'],['D','E','F'],['G']]
>>> [['A','B','D'],['C','E','G'],['F']]
>>> [['A','B','D'],['C','F','G'],['E']]
>>> [['A','B','F'],['C','E','G'],['D']]
>>> [['A','E','G'],['B','D','F'],['C']]
>>> and so on...
You can use a recursive function to keep getting a combination of 3 from the remaining list of people until the list is exhausted:
from itertools import combinations
def get_teams(list_of_ppl, size=3):
if len(list_of_ppl) > size:
for team in combinations(list_of_ppl, size):
for teams in get_teams(list(set(list_of_ppl) - set(team))):
yield [list(team), *teams]
else:
yield [list_of_ppl]
for team in get_teams(list_of_ppl):
print(team)
so that with list_of_ppl = ['A','B','C','D','E','F'], this outputs:
[['A', 'B', 'C'], ['F', 'E', 'D']]
[['A', 'B', 'D'], ['C', 'F', 'E']]
[['A', 'B', 'E'], ['C', 'F', 'D']]
[['A', 'B', 'F'], ['C', 'E', 'D']]
[['A', 'C', 'D'], ['F', 'B', 'E']]
[['A', 'C', 'E'], ['F', 'B', 'D']]
[['A', 'C', 'F'], ['B', 'E', 'D']]
[['A', 'D', 'E'], ['C', 'B', 'F']]
[['A', 'D', 'F'], ['C', 'B', 'E']]
[['A', 'E', 'F'], ['C', 'B', 'D']]
[['B', 'C', 'D'], ['E', 'F', 'A']]
[['B', 'C', 'E'], ['F', 'A', 'D']]
[['B', 'C', 'F'], ['E', 'A', 'D']]
[['B', 'D', 'E'], ['C', 'F', 'A']]
[['B', 'D', 'F'], ['C', 'A', 'E']]
[['B', 'E', 'F'], ['C', 'A', 'D']]
[['C', 'D', 'E'], ['F', 'B', 'A']]
[['C', 'D', 'F'], ['E', 'B', 'A']]
[['C', 'E', 'F'], ['B', 'A', 'D']]
[['D', 'E', 'F'], ['C', 'B', 'A']]
or with a list_of_ppl whose length is not divisible by 3 such as list_of_ppl = ['A','B','C','D','E','F','G'], this outputs:
[['A', 'B', 'C'], ['E', 'G', 'F'], ['D']]
[['A', 'B', 'C'], ['E', 'G', 'D'], ['F']]
[['A', 'B', 'C'], ['E', 'F', 'D'], ['G']]
[['A', 'B', 'C'], ['G', 'F', 'D'], ['E']]
[['A', 'B', 'D'], ['E', 'C', 'G'], ['F']]
[['A', 'B', 'D'], ['E', 'C', 'F'], ['G']]
[['A', 'B', 'D'], ['E', 'G', 'F'], ['C']]
[['A', 'B', 'D'], ['C', 'G', 'F'], ['E']]
[['A', 'B', 'E'], ['C', 'G', 'F'], ['D']]
[['A', 'B', 'E'], ['C', 'G', 'D'], ['F']]
[['A', 'B', 'E'], ['C', 'F', 'D'], ['G']]
[['A', 'B', 'E'], ['G', 'F', 'D'], ['C']]
[['A', 'B', 'F'], ['E', 'C', 'G'], ['D']]
[['A', 'B', 'F'], ['E', 'C', 'D'], ['G']]
[['A', 'B', 'F'], ['E', 'G', 'D'], ['C']]
[['A', 'B', 'F'], ['C', 'G', 'D'], ['E']]
[['A', 'B', 'G'], ['E', 'C', 'F'], ['D']]
[['A', 'B', 'G'], ['E', 'C', 'D'], ['F']]
[['A', 'B', 'G'], ['E', 'F', 'D'], ['C']]
[['A', 'B', 'G'], ['C', 'F', 'D'], ['E']]
[['A', 'C', 'D'], ['B', 'E', 'G'], ['F']]
[['A', 'C', 'D'], ['B', 'E', 'F'], ['G']]
[['A', 'C', 'D'], ['B', 'G', 'F'], ['E']]
[['A', 'C', 'D'], ['E', 'G', 'F'], ['B']]
[['A', 'C', 'E'], ['B', 'G', 'F'], ['D']]
[['A', 'C', 'E'], ['B', 'G', 'D'], ['F']]
[['A', 'C', 'E'], ['B', 'F', 'D'], ['G']]
[['A', 'C', 'E'], ['G', 'F', 'D'], ['B']]
[['A', 'C', 'F'], ['B', 'E', 'G'], ['D']]
[['A', 'C', 'F'], ['B', 'E', 'D'], ['G']]
[['A', 'C', 'F'], ['B', 'G', 'D'], ['E']]
[['A', 'C', 'F'], ['E', 'G', 'D'], ['B']]
[['A', 'C', 'G'], ['B', 'E', 'F'], ['D']]
[['A', 'C', 'G'], ['B', 'E', 'D'], ['F']]
[['A', 'C', 'G'], ['B', 'F', 'D'], ['E']]
[['A', 'C', 'G'], ['E', 'F', 'D'], ['B']]
[['A', 'D', 'E'], ['B', 'C', 'G'], ['F']]
[['A', 'D', 'E'], ['B', 'C', 'F'], ['G']]
[['A', 'D', 'E'], ['B', 'G', 'F'], ['C']]
[['A', 'D', 'E'], ['C', 'G', 'F'], ['B']]
[['A', 'D', 'F'], ['B', 'E', 'C'], ['G']]
[['A', 'D', 'F'], ['B', 'E', 'G'], ['C']]
[['A', 'D', 'F'], ['B', 'C', 'G'], ['E']]
[['A', 'D', 'F'], ['E', 'C', 'G'], ['B']]
[['A', 'D', 'G'], ['B', 'E', 'C'], ['F']]
[['A', 'D', 'G'], ['B', 'E', 'F'], ['C']]
[['A', 'D', 'G'], ['B', 'C', 'F'], ['E']]
[['A', 'D', 'G'], ['E', 'C', 'F'], ['B']]
[['A', 'E', 'F'], ['B', 'C', 'G'], ['D']]
[['A', 'E', 'F'], ['B', 'C', 'D'], ['G']]
[['A', 'E', 'F'], ['B', 'G', 'D'], ['C']]
[['A', 'E', 'F'], ['C', 'G', 'D'], ['B']]
[['A', 'E', 'G'], ['B', 'C', 'F'], ['D']]
[['A', 'E', 'G'], ['B', 'C', 'D'], ['F']]
[['A', 'E', 'G'], ['B', 'F', 'D'], ['C']]
[['A', 'E', 'G'], ['C', 'F', 'D'], ['B']]
[['A', 'F', 'G'], ['B', 'E', 'C'], ['D']]
[['A', 'F', 'G'], ['B', 'E', 'D'], ['C']]
[['A', 'F', 'G'], ['B', 'C', 'D'], ['E']]
[['A', 'F', 'G'], ['E', 'C', 'D'], ['B']]
[['B', 'C', 'D'], ['E', 'A', 'G'], ['F']]
[['B', 'C', 'D'], ['E', 'A', 'F'], ['G']]
[['B', 'C', 'D'], ['E', 'G', 'F'], ['A']]
[['B', 'C', 'D'], ['A', 'G', 'F'], ['E']]
[['B', 'C', 'E'], ['A', 'G', 'F'], ['D']]
[['B', 'C', 'E'], ['A', 'G', 'D'], ['F']]
[['B', 'C', 'E'], ['A', 'F', 'D'], ['G']]
[['B', 'C', 'E'], ['G', 'F', 'D'], ['A']]
[['B', 'C', 'F'], ['E', 'A', 'G'], ['D']]
[['B', 'C', 'F'], ['E', 'A', 'D'], ['G']]
[['B', 'C', 'F'], ['E', 'G', 'D'], ['A']]
[['B', 'C', 'F'], ['A', 'G', 'D'], ['E']]
[['B', 'C', 'G'], ['E', 'A', 'F'], ['D']]
[['B', 'C', 'G'], ['E', 'A', 'D'], ['F']]
[['B', 'C', 'G'], ['E', 'F', 'D'], ['A']]
[['B', 'C', 'G'], ['A', 'F', 'D'], ['E']]
[['B', 'D', 'E'], ['C', 'A', 'G'], ['F']]
[['B', 'D', 'E'], ['C', 'A', 'F'], ['G']]
[['B', 'D', 'E'], ['C', 'G', 'F'], ['A']]
[['B', 'D', 'E'], ['A', 'G', 'F'], ['C']]
[['B', 'D', 'F'], ['E', 'C', 'A'], ['G']]
[['B', 'D', 'F'], ['E', 'C', 'G'], ['A']]
[['B', 'D', 'F'], ['E', 'A', 'G'], ['C']]
[['B', 'D', 'F'], ['C', 'A', 'G'], ['E']]
[['B', 'D', 'G'], ['E', 'C', 'A'], ['F']]
[['B', 'D', 'G'], ['E', 'C', 'F'], ['A']]
[['B', 'D', 'G'], ['E', 'A', 'F'], ['C']]
[['B', 'D', 'G'], ['C', 'A', 'F'], ['E']]
[['B', 'E', 'F'], ['C', 'A', 'G'], ['D']]
[['B', 'E', 'F'], ['C', 'A', 'D'], ['G']]
[['B', 'E', 'F'], ['C', 'G', 'D'], ['A']]
[['B', 'E', 'F'], ['A', 'G', 'D'], ['C']]
[['B', 'E', 'G'], ['C', 'A', 'F'], ['D']]
[['B', 'E', 'G'], ['C', 'A', 'D'], ['F']]
[['B', 'E', 'G'], ['C', 'F', 'D'], ['A']]
[['B', 'E', 'G'], ['A', 'F', 'D'], ['C']]
[['B', 'F', 'G'], ['E', 'C', 'A'], ['D']]
[['B', 'F', 'G'], ['E', 'C', 'D'], ['A']]
[['B', 'F', 'G'], ['E', 'A', 'D'], ['C']]
[['B', 'F', 'G'], ['C', 'A', 'D'], ['E']]
[['C', 'D', 'E'], ['B', 'A', 'G'], ['F']]
[['C', 'D', 'E'], ['B', 'A', 'F'], ['G']]
[['C', 'D', 'E'], ['B', 'G', 'F'], ['A']]
[['C', 'D', 'E'], ['A', 'G', 'F'], ['B']]
[['C', 'D', 'F'], ['B', 'E', 'A'], ['G']]
[['C', 'D', 'F'], ['B', 'E', 'G'], ['A']]
[['C', 'D', 'F'], ['B', 'A', 'G'], ['E']]
[['C', 'D', 'F'], ['E', 'A', 'G'], ['B']]
[['C', 'D', 'G'], ['B', 'E', 'A'], ['F']]
[['C', 'D', 'G'], ['B', 'E', 'F'], ['A']]
[['C', 'D', 'G'], ['B', 'A', 'F'], ['E']]
[['C', 'D', 'G'], ['E', 'A', 'F'], ['B']]
[['C', 'E', 'F'], ['B', 'A', 'G'], ['D']]
[['C', 'E', 'F'], ['B', 'A', 'D'], ['G']]
[['C', 'E', 'F'], ['B', 'G', 'D'], ['A']]
[['C', 'E', 'F'], ['A', 'G', 'D'], ['B']]
[['C', 'E', 'G'], ['B', 'A', 'F'], ['D']]
[['C', 'E', 'G'], ['B', 'A', 'D'], ['F']]
[['C', 'E', 'G'], ['B', 'F', 'D'], ['A']]
[['C', 'E', 'G'], ['A', 'F', 'D'], ['B']]
[['C', 'F', 'G'], ['B', 'E', 'A'], ['D']]
[['C', 'F', 'G'], ['B', 'E', 'D'], ['A']]
[['C', 'F', 'G'], ['B', 'A', 'D'], ['E']]
[['C', 'F', 'G'], ['E', 'A', 'D'], ['B']]
[['D', 'E', 'F'], ['B', 'C', 'A'], ['G']]
[['D', 'E', 'F'], ['B', 'C', 'G'], ['A']]
[['D', 'E', 'F'], ['B', 'A', 'G'], ['C']]
[['D', 'E', 'F'], ['C', 'A', 'G'], ['B']]
[['D', 'E', 'G'], ['B', 'C', 'A'], ['F']]
[['D', 'E', 'G'], ['B', 'C', 'F'], ['A']]
[['D', 'E', 'G'], ['B', 'A', 'F'], ['C']]
[['D', 'E', 'G'], ['C', 'A', 'F'], ['B']]
[['D', 'F', 'G'], ['B', 'E', 'C'], ['A']]
[['D', 'F', 'G'], ['B', 'E', 'A'], ['C']]
[['D', 'F', 'G'], ['B', 'C', 'A'], ['E']]
[['D', 'F', 'G'], ['E', 'C', 'A'], ['B']]
[['E', 'F', 'G'], ['B', 'C', 'A'], ['D']]
[['E', 'F', 'G'], ['B', 'C', 'D'], ['A']]
[['E', 'F', 'G'], ['B', 'A', 'D'], ['C']]
[['E', 'F', 'G'], ['C', 'A', 'D'], ['B']]
Use combinations from itertools:
from itertools import combinations
list(map(list,combinations(list_of_ppl,3)))
[['A', 'B', 'C'],
['A', 'B', 'D'],
['A', 'B', 'E'],
['A', 'B', 'F'],
['A', 'C', 'D'],
['A', 'C', 'E'],
['A', 'C', 'F'],
['A', 'D', 'E'],
['A', 'D', 'F'],
['A', 'E', 'F'],
['B', 'C', 'D'],
['B', 'C', 'E'],
['B', 'C', 'F'],
['B', 'D', 'E'],
['B', 'D', 'F'],
['B', 'E', 'F'],
['C', 'D', 'E'],
['C', 'D', 'F'],
['C', 'E', 'F'],
['D', 'E', 'F']]
Or:
l = list(map(list,combinations(list_of_ppl,3)))
list(map(list,zip(l[::2],l[::-1][::2])))
[[['A', 'B', 'C'], ['D', 'E', 'F']],
[['A', 'B', 'E'], ['C', 'D', 'F']],
[['A', 'C', 'D'], ['B', 'E', 'F']],
[['A', 'C', 'F'], ['B', 'D', 'E']],
[['A', 'D', 'F'], ['B', 'C', 'E']],
[['B', 'C', 'D'], ['A', 'E', 'F']],
[['B', 'C', 'F'], ['A', 'D', 'E']],
[['B', 'D', 'F'], ['A', 'C', 'E']],
[['C', 'D', 'E'], ['A', 'B', 'F']],
[['C', 'E', 'F'], ['A', 'B', 'D']]]
Or:
list(map(list,zip(l,l[::-1])))
[[['A', 'B', 'C'], ['D', 'E', 'F']],
[['A', 'B', 'D'], ['C', 'E', 'F']],
[['A', 'B', 'E'], ['C', 'D', 'F']],
[['A', 'B', 'F'], ['C', 'D', 'E']],
[['A', 'C', 'D'], ['B', 'E', 'F']],
[['A', 'C', 'E'], ['B', 'D', 'F']],
[['A', 'C', 'F'], ['B', 'D', 'E']],
[['A', 'D', 'E'], ['B', 'C', 'F']],
[['A', 'D', 'F'], ['B', 'C', 'E']],
[['A', 'E', 'F'], ['B', 'C', 'D']],
[['B', 'C', 'D'], ['A', 'E', 'F']],
[['B', 'C', 'E'], ['A', 'D', 'F']],
[['B', 'C', 'F'], ['A', 'D', 'E']],
[['B', 'D', 'E'], ['A', 'C', 'F']],
[['B', 'D', 'F'], ['A', 'C', 'E']],
[['B', 'E', 'F'], ['A', 'C', 'D']],
[['C', 'D', 'E'], ['A', 'B', 'F']],
[['C', 'D', 'F'], ['A', 'B', 'E']],
[['C', 'E', 'F'], ['A', 'B', 'D']],
[['D', 'E', 'F'], ['A', 'B', 'C']]]
for v in list(map(list,zip(l,l[::-1]))):
print(v)
[['A', 'B', 'C'], ['D', 'E', 'F']]
[['A', 'B', 'D'], ['C', 'E', 'F']]
[['A', 'B', 'E'], ['C', 'D', 'F']]
[['A', 'B', 'F'], ['C', 'D', 'E']]
[['A', 'C', 'D'], ['B', 'E', 'F']]
[['A', 'C', 'E'], ['B', 'D', 'F']]
[['A', 'C', 'F'], ['B', 'D', 'E']]
[['A', 'D', 'E'], ['B', 'C', 'F']]
[['A', 'D', 'F'], ['B', 'C', 'E']]
[['A', 'E', 'F'], ['B', 'C', 'D']]
[['B', 'C', 'D'], ['A', 'E', 'F']]
[['B', 'C', 'E'], ['A', 'D', 'F']]
[['B', 'C', 'F'], ['A', 'D', 'E']]
[['B', 'D', 'E'], ['A', 'C', 'F']]
[['B', 'D', 'F'], ['A', 'C', 'E']]
[['B', 'E', 'F'], ['A', 'C', 'D']]
[['C', 'D', 'E'], ['A', 'B', 'F']]
[['C', 'D', 'F'], ['A', 'B', 'E']]
[['C', 'E', 'F'], ['A', 'B', 'D']]
[['D', 'E', 'F'], ['A', 'B', 'C']]
the built-in itertools has a helper function for doing it.
itertools.combinations
see doc
itertools.combinations(iterable, r)
Return r length subsequences of elements from the input iterable.
Combinations are emitted in lexicographic sort order. So, if the input >iterable is sorted, the combination tuples will be produced in sorted order.
Elements are treated as unique based on their position, not on their value. So if the input elements are unique, there will be no repeat values in each combination.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
The issue occurred as continuation of my experiments with the graph theory, so as an input I will also add the picture of my graph:
But I also have a description of this graph in several arrays:
First of all, array with all connections:
arrAllConnections = [['F', 'G'], ['G', 'F'], ['G', 'N'], ['N', 'G'], ['N', 'E'], ['E', 'N'], ['E', 'D'], ['D', 'E'], ['D', 'C'], ['C', 'D'], ['C', 'B'], ['B', 'C'], ['B', 'A'], ['A', 'B'], ['A', 'E'], ['E', 'A'], ['B', 'X8'], ['X8', 'B'], ['X8', 'X3'], ['X3', 'X8'], ['X3', 'X2'], ['X2', 'X3'], ['C', 'T'], ['T', 'C'], ['T', 'T1'], ['T1', 'T'], ['T1', 'Y'], ['Y', 'T1'], ['Y', 'L'], ['L', 'Y'],['L', 'P'], ['P', 'L'], ['P', 'Z'], ['Z', 'P'], ['Z', 'Y'], ['Y', 'Z'], ['L', 'K3'], ['K3', 'L'], ['Z', 'K1'], ['K1', 'Z'], ['K1', 'K2'], ['K2', 'K1']]
I thought that it contains some extra information and added such a line:
arrAllConnections = [list(i) for i in set(map(tuple, arrAllConnections))]
Also there are some nodes which I want to connect to the other node groups. Node groups are cycles and here is the list of cycles:
arrWithWhatToConnect = [['A', 'B', 'C', 'D', 'E'], ['Z', 'P', 'L', 'Y']]
Nodes which to connect are mostly nodes outside the cycles, but they may be 'inside' (for example: 'E' node):
arrWhatToConnect = ['F', 'G', 'N', 'X2', 'X3', 'K3', 'K1', 'K2', 'E']
If you look at the image you may notice several things:
X2 is connected to X3, which is connected to X8, but because the latter is not in the list arrWhatToConnect the process must be stopped;
E node is already in the cycle, so the process will be stopped as soon as E will be found in the cycle;
For other nodes, e.g. F a recursion is needed, in brief it should be smth like that: if F not in cycle/arrWithWhatToConnect, but in arrWhatToConnect -> check next node and repeat check for cycle/arrWithWhatToConnect or/|| arrWhatToConnect.
Finally, here is what I am trying to get:
for 'E' node -> ['A', 'B', 'C', 'D', 'E']
for 'F' node -> ['F', 'G', 'N', 'A', 'B', 'C', 'D', 'E']
etc
These are the lists containing the shortest pathes between chosen nodes and cycles.
My code:
arrWhatToConnect = ['F', 'G', 'N', 'X2', 'X3', 'K3', 'K1', 'K2', 'E']
#THERE IS NO X8!!!!!
arrWithWhatToConnect = [['A', 'B', 'C', 'D', 'E'], ['Z', 'P', 'L', 'Y']]
arrAllConnections = [['F', 'G'], ['G', 'F'], ['G', 'N'], ['N', 'G'], ['N', 'E'], ['E', 'N'], ['E', 'D'], ['D', 'E'], ['D', 'C'], ['C', 'D'], ['C', 'B'], ['B', 'C'], ['B', 'A'], ['A', 'B'], ['A', 'E'], ['E', 'A'], ['B', 'X8'], ['X8', 'B'], ['X8', 'X3'], ['X3', 'X8'], ['X3', 'X2'], ['X2', 'X3'], ['C', 'T'], ['T', 'C'], ['T', 'T1'], ['T1', 'T'], ['T1', 'Y'], ['Y', 'T1'], ['Y', 'L'], ['L', 'Y'],['L', 'P'], ['P', 'L'], ['P', 'Z'], ['Z', 'P'], ['Z', 'Y'], ['Y', 'Z'], ['L', 'K3'], ['K3', 'L'], ['Z', 'K1'], ['K1', 'Z'], ['K1', 'K2'], ['K2', 'K1']]
nullFirstConnect = []
#arrAllConnections = (sorted(item for item in arrAllConnections))
arrAllConnections = [list(i) for i in set(map(tuple, arrAllConnections))]
print(arrAllConnections, 'REMOVED?')
def connect(arrWithWhatToConnect, arrWhatToConnect, arrAllConnections, item, olderItems):
arrAllConDel = arrAllConnections
print('CONTENTS OF TRUNCATED ARR CONNECTIONS', arrAllConDel)
arrConnected = []
for cycle in arrWithWhatToConnect:
if item in cycle:
arrConnected.extend(cycle)
arrConnected.extend(olderItems)
print('My item in cycle', item, cycle, arrConnected)
else:
print('Not in cycle', item)
print('GOING TO CHECK ELSEWHERE!')
olderItems.extend(item)
olderItems.extend(olderItems)
for connection in arrAllConnections:
item1 = connection[0]
item2 = connection[1]
if item in connection:
if item == item1:
print('connection is', connection, 'item', item, 'item1', item1, 'item2', item2)
if item2 in arrWhatToConnect:
del arrAllConDel[arrAllConnections.index(connection)]
connect(arrWithWhatToConnect, arrWhatToConnect, arrAllConDel, item2, olderItems)
elif item == item2:
if item1 in arrWhatToConnect:
del arrAllConDel[arrAllConnections.index(connection)]
connect(arrWithWhatToConnect, arrWhatToConnect, arrAllConDel, item1, olderItems)
return arrConnected
for item in arrWhatToConnect:
print(item)
print(connect(arrWithWhatToConnect, arrWhatToConnect, arrAllConnections, item, nullFirstConnect))
It is my first attempt to write any recursion (or if I don't remember it is a realised attempt to write recursion ;)). Everything seems to be more or less clear and even working. But there are not enough return statements, which has an impact on memory. As well as it seems that I should add visitedNodes array. If you can show me how to improve my code please do it. Thanks. Preferably in python without any modules such as nx etc, I would like to understand what is going on inside.