Python - Speed up generation of permutations of a list (and process of checking if permuations in Dict) -
i need faster way generate permutations of list, check if each 1 in dictionary.
x in range (max_combo_len, 0, -1): possible_combos = [] permutations = list(itertools.permutations(bag,x)) item in permutations: possible_combos.append(" ".join(item)) #then check see if each possible combo in specific dict
if helps, lists going lists of strings. ['such as', 'this', 'one']
my solution works, it's slow. need stop using python, thought i'd run experts first!
best, gary
i can't test without better input cases, here few improvements:
for x in xrange(max_combo_len, 0, -1): possible_combos = (" ".join(item) item in itertools.permutations(bag,x)) #then check see if each possible combo in specific dict combos = (c c in possible_combos if c in specific_dict)
first, assuming you're using python 2.x, xrange
not constructing explicit list, rather yielding each x
need it.
more importantly, can throw main effort generator expressions , have yield values on demand.
Comments
Post a Comment