Get combinations of list without replacement in Python - python
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.
Related
How to remove all duplicates in a 3D Array in Phyton?
So i have an array like this one: [[['A'], ['C']], [['B', 'E'], ['G']], [['C', 'D'], ['E', 'F']], [['C', 'D'], ['F', 'E']], [['D', 'C'], ['E', 'F']], [['D', 'C'], ['F', 'E']], [['E', 'B'], ['G']], [['F'], ['H']]] Now i want to remove all the duplicates in the array while different sequences dont matter so CD is the same as DC so the result should look like this: [[['A'], ['C']], [['B', 'E'], ['G']], [['C', 'D'], ['E', 'F']],[['F'], ['H']]] How should i do this i thought about three for loops, but is there a simple way to do this? can someone help?
Here's one way to do this: arr = [[['A'], ['C']], [['B', 'E'], ['G']], [['C', 'D'], ['E', 'F']], [['C', 'D'], ['F', 'E']], [['D', 'C'], ['E', 'F']], [['D', 'C'], ['F', 'E']], [['E', 'B'], ['G']], [['F'], ['H']]] seen = set() for a1 in arr: for i in range(len(a1))[::-1]: a2 = tuple(sorted(a1[i])) if a2 in seen: a1.pop(i) else: seen.add(a2) arr = [*filter(lambda x:x,arr)] print(arr) Result: [[['A'], ['C']], [['B', 'E'], ['G']], [['C', 'D'], ['E', 'F']], [['F'], ['H']]]
Python program running incorrectly
I am trying to run the following program as part of my other program n=2 array=[[[[[[['*' for col in range(6)] for col in range(n)] for col in range(n)] for col in range(n)] for row in range(n)] for row in range(n)]for row in range(n)] possible=[[[[[[[] for col in range(n)] for col in range(n)] for col in range(n)] for row in range(n)] for row in range(n)] for row in range(n)] possible[0][0][0][0][0][0]=[['a', 'f', 'b', 'c', 'd', 'e'], ['a', 'f', 'b', 'c', 'e', 'd'], ['a', 'f', 'b', 'd', 'c', 'e'], ['a', 'f', 'b', 'e', 'c', 'd'], ['a', 'f', 'c', 'b', 'd', 'e'], ['a', 'f', 'c', 'b', 'e', 'd'], ['a', 'f', 'd', 'b', 'c', 'e'], ['a', 'f', 'e', 'b', 'c', 'd'], ['a', 'f', 'c', 'd', 'b', 'e'], ['a', 'f', 'c', 'e', 'b', 'd'], ['a', 'f', 'd', 'c', 'b', 'e'], ['a', 'f', 'e', 'c', 'b', 'd'], ['a', 'e', 'b', 'f', 'c', 'd'], ['a', 'e', 'b', 'c', 'f', 'd'], ['a', 'e', 'c', 'f', 'b', 'd'], ['a', 'e', 'c', 'b', 'f', 'd'], ['f', 'a', 'b', 'c', 'd', 'e'], ['f', 'a', 'b', 'c', 'd', 'e'], ['f', 'a', 'b', 'c', 'd', 'e'], ['f', 'a', 'b', 'c', 'd', 'e'], ['f', 'a', 'b', 'c', 'd', 'e'], ['f', 'a', 'b', 'c', 'd', 'e'], ['f', 'a', 'b', 'c', 'd', 'e'], ['f', 'a', 'b', 'c', 'd', 'e'], ['f', 'a', 'b', 'c', 'd', 'e'], ['f', 'a', 'b', 'c', 'd', 'e'], ['f', 'a', 'b', 'c', 'd', 'e'], ['f', 'a', 'b', 'c', 'd', 'e']] array[1][0][0][0][0][0]=['a', 'b', 'f', 'd', 'e', 'c'] array[0][0][0][0][0][0]=['*', '*', 'f', 'c', 'd', '*'] r=[] for s in range(6): if s!=0: if array[0][0][0][0][0][0][s]!='*': r.append(s) x=[] for y in range(6): if y!=0: if array[1][0][0][0][0][0][y]!='*': x.append(y) t=list(set(x).intersection(set(r))) for j in t: if (array[1][0][0][0][0][0][j]!=array[0][0][0][0][0][0][j]): for i in possible[0][0][0][0][0][0]: if i[0]==array[1][0][0][0][0][0][0]: possible[0][0][0][0][0][0].remove(i) Output is [['a', 'f', 'b', 'e', 'c', 'd'], ['a', 'f', 'e', 'b', 'c', 'd'], ['a', 'f', 'e', 'c', 'b', 'd'], ['a', 'e', 'c', 'b', 'f', 'd'], ['f', 'a', 'b', 'c', 'd', 'e'], ['f', 'a', 'b', 'c', 'd', 'e'], ['f', 'a', 'b', 'c', 'd', 'e'], ['f', 'a', 'b', 'c', 'd', 'e'], ['f', 'a', 'b', 'c', 'd', 'e'], ['f', 'a', 'b', 'c', 'd', 'e'], ['f', 'a', 'b', 'c', 'd', 'e'], ['f', 'a', 'b', 'c', 'd', 'e'], ['f', 'a', 'b', 'c', 'd', 'e'], ['f', 'a', 'b', 'c', 'd', 'e'], ['f', 'a', 'b', 'c', 'd', 'e'], ['f', 'a', 'b', 'c', 'd', 'e']] print(possible[0][0][0][0][0][0]) should not have any list starting with 'a'. I don't understand what I am doing wrong.
how to display students in increasing order based on correct answers in python
okay so I'm writing this code but am not sure how to put the students in order of least to greatest correct answers. The out put should be "Student 3's number of correct answers is 4" and so on. Here's my current code: def main(): answers = [ ['A', 'B', 'A', 'C', 'C', 'D', 'E', 'E', 'A', 'D'], ['D', 'B', 'A', 'B', 'C', 'A', 'E', 'E', 'A', 'D'], ['E', 'D', 'D', 'A', 'C', 'B', 'E', 'E', 'A', 'D'], ['C', 'B', 'A', 'E', 'D', 'C', 'E', 'E', 'A', 'D'], ['A', 'B', 'D', 'C', 'C', 'D', 'E', 'E', 'A', 'D'], ['B', 'B', 'E', 'C', 'C', 'D', 'E', 'E', 'A', 'D'], ['B', 'B', 'A', 'C', 'C', 'D', 'E', 'E', 'A', 'D'], ['E', 'B', 'E', 'C', 'C', 'D', 'E', 'E', 'A', 'D']] key = ['D', 'B', 'D', 'C', 'C', 'D', 'A', 'E', 'A', 'D'] for i in range(len(answers)): correctCount = 0 for j in range(len(answers[i])): if answers[i][j] == key[j]: correctCount +=1 print("Student", i, "'s correct count is", correctCount) main()
I think is best you split the problem in steps: Determine the number of correct answers Sort the output Given the previous steps, you could do the following: answers = [ ['A', 'B', 'A', 'C', 'C', 'D', 'E', 'E', 'A', 'D'], ['D', 'B', 'A', 'B', 'C', 'A', 'E', 'E', 'A', 'D'], ['E', 'D', 'D', 'A', 'C', 'B', 'E', 'E', 'A', 'D'], ['C', 'B', 'A', 'E', 'D', 'C', 'E', 'E', 'A', 'D'], ['A', 'B', 'D', 'C', 'C', 'D', 'E', 'E', 'A', 'D'], ['B', 'B', 'E', 'C', 'C', 'D', 'E', 'E', 'A', 'D'], ['B', 'B', 'A', 'C', 'C', 'D', 'E', 'E', 'A', 'D'], ['E', 'B', 'E', 'C', 'C', 'D', 'E', 'E', 'A', 'D']] def number_of_correct_answers(answers, golden=['D', 'B', 'D', 'C', 'C', 'D', 'A', 'E', 'A', 'D']): """Return the number of correct answers given a gold standard""" return sum(g == a for g, a in zip(golden, answers)) for student_answer in sorted(answers, key=number_of_correct_answers, reverse=True): # the best come first print(student_answer, 'Number of correct', number_of_correct_answers(student_answer)) Output ['A', 'B', 'D', 'C', 'C', 'D', 'E', 'E', 'A', 'D'] Number of correct 8 ['A', 'B', 'A', 'C', 'C', 'D', 'E', 'E', 'A', 'D'] Number of correct 7 ['B', 'B', 'E', 'C', 'C', 'D', 'E', 'E', 'A', 'D'] Number of correct 7 ['B', 'B', 'A', 'C', 'C', 'D', 'E', 'E', 'A', 'D'] Number of correct 7 ['E', 'B', 'E', 'C', 'C', 'D', 'E', 'E', 'A', 'D'] Number of correct 7 ['D', 'B', 'A', 'B', 'C', 'A', 'E', 'E', 'A', 'D'] Number of correct 6 ['E', 'D', 'D', 'A', 'C', 'B', 'E', 'E', 'A', 'D'] Number of correct 5 ['C', 'B', 'A', 'E', 'D', 'C', 'E', 'E', 'A', 'D'] Number of correct 4 The key of the functionaly above relies on the functions: sum and sorted. In the example above the better students come first, if you want the other way around just remove the reverse=True parameter. See: Sorting - HOWTO
Python: Get all the non-overlapping, consecutive sublists from a list
I'm trying to figure out an elegant/efficient solution for finding sublists in a list, with certain constraints. For example, given this list: values = ['a', 'b', 'c', 'd', 'e', 'f'] And a value for maxLength: maxLength = 4 Find all sublists in the values list such that: Sublist length is between 1 and maxLength Sublists are consecutive Sublists are non-overlapping So in this example, these are acceptable solutions: [['a'], ['b'], ['c'], ['d'], ['e'], ['f']] [['a', 'b'], ['c'], ['d'], ['e'], ['f']] ... [['a', 'b'], ['c', 'd', 'e'], ['f']] ... [['a', 'b', 'c', 'd'], ['e', 'f']] ... [['a', 'b'], ['c', 'd', 'e', 'f']] These are not acceptable: [['a', 'b'], ['d', 'e'], ['f']] #Error: not consecutive, 'c' is missing [['a', 'b', 'c'], ['c', 'd', 'e'], ['f']] #Error: overlapping, 'c' is in two subsets [['a', 'b', 'c', 'd', 'e'], ['f']] #Error: the first sublist has length > maxLength (4)
You can use recursion: values = ['a', 'b', 'c', 'd', 'e', 'f'] maxLength = 4 def groupings(d): if not d: yield [] else: for i in range(1, maxLength+1): for c in groupings(d[i:]): yield [d[:i], *c] _d = list(groupings(values)) new_d = [a for i, a in enumerate(_d) if a not in _d[:i]] Output: [[['a'], ['b'], ['c'], ['d'], ['e'], ['f']], [['a'], ['b'], ['c'], ['d'], ['e', 'f']], [['a'], ['b'], ['c'], ['d', 'e'], ['f']], [['a'], ['b'], ['c'], ['d', 'e', 'f']], [['a'], ['b'], ['c', 'd'], ['e'], ['f']], [['a'], ['b'], ['c', 'd'], ['e', 'f']], [['a'], ['b'], ['c', 'd', 'e'], ['f']], [['a'], ['b'], ['c', 'd', 'e', 'f']], [['a'], ['b', 'c'], ['d'], ['e'], ['f']], [['a'], ['b', 'c'], ['d'], ['e', 'f']], [['a'], ['b', 'c'], ['d', 'e'], ['f']], [['a'], ['b', 'c'], ['d', 'e', 'f']], [['a'], ['b', 'c', 'd'], ['e'], ['f']], [['a'], ['b', 'c', 'd'], ['e', 'f']], [['a'], ['b', 'c', 'd', 'e'], ['f']], [['a', 'b'], ['c'], ['d'], ['e'], ['f']], [['a', 'b'], ['c'], ['d'], ['e', 'f']], [['a', 'b'], ['c'], ['d', 'e'], ['f']], [['a', 'b'], ['c'], ['d', 'e', 'f']], [['a', 'b'], ['c', 'd'], ['e'], ['f']], [['a', 'b'], ['c', 'd'], ['e', 'f']], [['a', 'b'], ['c', 'd', 'e'], ['f']], [['a', 'b'], ['c', 'd', 'e', 'f']], [['a', 'b', 'c'], ['d'], ['e'], ['f']], [['a', 'b', 'c'], ['d'], ['e', 'f']], [['a', 'b', 'c'], ['d', 'e'], ['f']], [['a', 'b', 'c'], ['d', 'e', 'f']], [['a', 'b', 'c', 'd'], ['e'], ['f']], [['a', 'b', 'c', 'd'], ['e', 'f']]]
Python 2d list sort by colum header
I have a 2d list: ['C', 'A', 'D', 'B'], #header row ['F', 'C', 'F', 'E'], #row 1 ['F', 'E', 'F', 'F'], #row 2 ['B', 'A', 'F', 'A'], ['A', 'F', 'C', 'E'], ['F', 'F', 'E', 'E'], ['C', 'E', 'F', 'C'] The first row in the list is the header row: C, A, D, B. How can I change this so that the columns are in order so it will appear: ['A', 'B', 'C', 'D'], #header row ['C', 'E', 'F', 'F'], #row 1 ['E', 'F', 'F', 'F'], #row 2 ['A', 'A', 'B', 'F'], ['F', 'E', 'A', 'C'], ['F', 'E', 'F', 'E'], ['E', 'C', 'C', 'F'] I want the headers to be in order A,B,C,D but also move the columns underneath with the header
You can use sorted and zip, first create a list of your column with zip(*a) then sort it (based on first index) with sorted, and again convert to first state with zip and convert the indices to list with map : >>> map(list,zip(*sorted(zip(*a)))) [['A', 'B', 'C', 'D'], ['C', 'E', 'F', 'F'], ['E', 'F', 'F', 'F'], ['A', 'A', 'B', 'F'], ['F', 'E', 'A', 'C'], ['F', 'E', 'F', 'E'], ['E', 'C', 'C', 'F']]
You could use numpy. >>> import numpy as np >>> data = np.array([['C', 'A', 'D', 'B'], #header row ... ['F', 'C', 'F', 'E'], #row 1 ... ['F', 'E', 'F', 'F'], #row 2 ... ['B', 'A', 'F', 'A'], ... ['A', 'F', 'C', 'E'], ... ['F', 'F', 'E', 'E'], ... ['C', 'E', 'F', 'C']]) >>> data[:,np.argsort(data[0])] # select sorted indices of first row as column indices. array([['A', 'B', 'C', 'D'], ['C', 'E', 'F', 'F'], ['E', 'F', 'F', 'F'], ['A', 'A', 'B', 'F'], ['F', 'E', 'A', 'C'], ['F', 'E', 'F', 'E'], ['E', 'C', 'C', 'F']], dtype='|S1')