I have a piece of code that requires hours to run on long lists :
for elt in listCombinaisons_survivor:
effects = []
for idBadge in elt:
effects.append(dictBadges[idBadge]["Effect"])
cpt = Counter(effects)
for type_effect in cpt.keys():
cpt_effect = cpt[type_effect]
if cpt_effect > 3:
listCombinaisons_survivor.remove(elt)
break
My listCombinaisons_survivor
contains all the possible combinations of 6 badges among 57 possibilities ([1,2,3,5,6,7],[1,2,3,5,6,8]...
).
For each possible combination, I pick in a dictionary the "Effect" matching the badge ID (Badge ID 1 = Damage), and I collect all the effects in the list "effects".
I then count how much occurencies of each effect I have, and if I have more than 3 badges with the same effect in a combination, I delete this combination.
Is there any way I could optimize this code to make it faster ?
I also tried this but this is not faster :
newlist_combinations = []
for elt in listCombinaisons_survivor:
effects = []
for idBadge in elt:
effects.append(dictBadges[idBadge]["Effect"])
cpt = Counter(effects)
for type_effect in cpt.keys():
cpt_effect = cpt[type_effect]
if cpt_effect > 3:
break
if not cpt_effect > 3:
newlist_combinations.append(elt)
I don't want to know how much time it takes, but how to optimize this code.
The whole code can be found there :
https://github.com/yirkkiller/Python/blob/master/badgesRepartition-NEW.py
Thanks !