Python program running incorrectly - python

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.

Related

Python global variable doesnt change

Example code:
podbor_dict = {}
def change(lst, sz):
lst_r = [lst[i:i+sz] for i in range(0, len(lst), sz)]
return lst_r
def qq():
global podbor_dict
if podbor_dict.get(
'podbor_'):
podbor = podbor_dict[
'podbor_']
podbor.clear()
else:
podbor_dict.update(
{'podbor_': []})
podbor = podbor_dict[
'podbor_']
podbor = ['a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', ]
podbor = change(podbor, 10)
print(podbor)
def zz():
global podbor_dict
podbor = podbor_dict[
'podbor_']
print(podbor)
print(podbor_dict)
qq()
zz()
Function 'change' adds every ten elements to a new array.
Expected output:
[['a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', 'a', 'b'], ['c', 'd', 'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd'], ['a', 'b', 'c', 'd', 'a', 'b', 'c', 'd']]
[['a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', 'a', 'b'], ['c', 'd', 'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd'], ['a', 'b', 'c', 'd', 'a', 'b', 'c', 'd']]
{'podbor_': [['a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', 'a', 'b'], ['c', 'd', 'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd'], ['a', 'b', 'c', 'd', 'a', 'b', 'c', 'd']]}
Real output:
[['a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', 'a', 'b'], ['c', 'd', 'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd'], ['a', 'b', 'c', 'd', 'a', 'b', 'c', 'd']]
[]
{'podbor_': []}
Several times changed function 'change' but nothing helped.
Really looking forward your answer.
Hey I found a solution
podbor_dict = {}
def change(lst, sz):
lst_r = [lst[i:i+sz] for i in range(0, len(lst), sz)]
return lst_r
def qq():
global podbor_dict
global podbor
if podbor_dict.get(
'podbor_'):
podbor = podbor_dict[
'podbor_']
podbor.clear()
else:
podbor_dict.update(
{'podbor_': []})
podbor = podbor_dict[
'podbor_']
podbor = ['a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', ]
podbor = change(podbor, 10)
print("fda" , podbor)
def zz():
global podbor_dict
global podbor
podbor_dict = {'podbor_': podbor}
print(podbor)
print(podbor_dict)
qq()
zz()
I made the variable podbor global and with the building from the dictionary you had an error
This is because you don't actually adding anything to it except for the 'podbor_' with empty list as value. Change the else block to
else:
podbor = ['a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd']
podbor = change(podbor, 10)
podbor_dict.update({'podbor_': podbor})
podbor = podbor_dict['podbor_']
# Output: [['a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', 'a', 'b'], ['c', 'd', 'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd'], ['a', 'b', 'c', 'd', 'a', 'b', 'c', 'd']]
# [['a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', 'a', 'b'], ['c', 'd', 'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd'], ['a', 'b', 'c', 'd', 'a', 'b', 'c', 'd']]
# {'podbor_': [['a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', 'a', 'b'], ['c', 'd', 'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd'], ['a', 'b', 'c', 'd', 'a', 'b', 'c', 'd']]}
podbor_dict = {}
def change(lst, sz):
lst_r = [lst[i:i+sz] for i in range(0, len(lst), sz)]
return lst_r
def qq():
global podbor_dict
if podbor_dict.get(
'podbor_'):
podbor_dict['podbor_'].clear()
# i also removed local variable declarations as they will be dropped at the end of the block anyway
else:
podbor_dict.update(
{'podbor_': []})
# we can say that if you assing value of dictionary to a variable, it will keep a reference to the list
# fi we then assign to this variable, we do not change a value in dictionary, but only the reference
# it is holding, thus dirrect assignment is needed
podbor = ['a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', ]
podbor_dict["podbor_"] = change(podbor, 10)
print(podbor_dict["podbor_"])
def zz():
global podbor_dict
podbor = podbor_dict[
'podbor_']
print(podbor)
print(podbor_dict)
qq()
zz()

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

Get combinations of list without replacement in 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.

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')

python compare values of the sublists of a list and add up values

I have a list with an unknown number of sublists.
I want to compare always one sublist with ALL other sublists.
If the values of the first sublist at position 0, 3 and 5 are equal to any other sublist, I want to to add together the values at position 7 in all matching lists.
And then add this first sublist with the (newly added up value) at position 7 to a new list.
list = [['A', 'a', 'b', 'B', 'c', 'd', 'C', '1'],
['A', 'a', 'b', 'B', 'c', 'd', 'C', '3'],
['D', 'r', 's', 'E', 't', 'u', 'F', '2'],
['A', 'a', 'b', 'B', 'c', 'd', 'C', '2'],
['D', 'r', 's', 'E', 't', 'u', 'F', '2'],.....]
wanted Output:
new_list = [['A', 'a', 'b', 'B', 'c', 'd', 'C', '6'],
['D', 'r', 's', 'E', 't', 'u', 'F', 4],...]
I wrote this code
def Inter(list):
a = 0
b = 3
c = 5
d = 0
x = []
for i in range(len(list)):
for y in range(len(list)):
if list[i][a] == list[y][a] and list[i][b] == list[y][b] and list[i][c] == list[y][c]:
IntSumtemp = []
IntSumtemp.append(str(float(list[i][7]) + float(list[y][7])))
x.append(list[i] + IntSumtemp)
del (x[d][7])
d +=1
else: None
return x
new_list= Inter(list)
but it gave this output:
new_list= [['A', 'a', 'b', 'B', 'c', 'd', 'C', '2.0'],
['A', 'a', 'b', 'B', 'c', 'd', 'C', '4.0'],
['A', 'a', 'b', 'B', 'c', 'd', 'C', '3.0'],
['A', 'a', 'b', 'B', 'c', 'd', 'C', '4.0'],
['A', 'a', 'b', 'B', 'c', 'd', 'C', '6.0'],
['A', 'a', 'b', 'B', 'c', 'd', 'C', '5.0'],
['D', 'r', 's', 'E', 't', 'u', 'F', '4.0'],
['D', 'r', 's', 'E', 't', 'u', 'F', '4.0'],
['A', 'a', 'b', 'B', 'c', 'd', 'C', '3.0'],
['A', 'a', 'b', 'B', 'c', 'd', 'C', '5.0'],
['A', 'a', 'b', 'B', 'c', 'd', 'C', '4.0'],
['D', 'r', 's', 'E', 't', 'u', 'F', '4.0'],
['D', 'r', 's', 'E', 't', 'u', 'F', '4.0']]
Can someone help me with this, please?
(Sorry, I am a absolute beginner, so if anything is unclear, plz ask...)
This approach is O(N) as opposed to yours which was O(N^2)
from collections import OrderedDict
from operator import itemgetter
items = [['A', 'a', 'b', 'B', 'c', 'd', 'C', '1'],
['A', 'a', 'b', 'B', 'c', 'd', 'C', '3'],
['D', 'r', 's', 'E', 't', 'u', 'F', '2'],
['A', 'a', 'b', 'B', 'c', 'd', 'C', '2'],
['D', 'r', 's', 'E', 't', 'u', 'F', '2']]
key = itemgetter(0, 3, 5)
d = OrderedDict()
for x in items:
d.setdefault(key(x), x[:7] + [0])[7] += int(x[7])
print d.values()
[['A', 'a', 'b', 'B', 'c', 'd', 'C', 6], ['D', 'r', 's', 'E', 't', 'u', 'F', 4]]

Categories

Resources