Can somebody explain to me why this piece of code does not give the desired result?
from itertools import permutations
red_pieces = ['RF', 'R3', 'RB', 'R2', 'R10', 'R9', 'R2', 'R1']
combinations = list(permutations(red_pieces, 8))
for combi in combinations:
if 'RF' in [combi[1], combi[3], combi[5], combi[7]]:
combinations.remove(combi)
In this piece of code I want to filter the tuples of all possible permutations of red_pieces (of length 8) based upon the position of the 'RF' element in the respective tuple. However, I noticed that this method does not seem to remove all tuples that meet the criteria.
Now I solved this problem by using a list comprehension like so:
def determine(combi):
return 'RF' in [combi[1], combi[3], combi[5], combi[7]]
listCombinations = [combi for combi in combinations if not determine(combi)]
But I am still quite interested why the first for loop does not work, as this seemed to me to be the most intuitive way to tackle this problem (albeit not the fastest)
Thanks!
Aucun commentaire:
Enregistrer un commentaire