samedi 10 novembre 2018

Picking a random element between two lists often and quickly in python

I have a bajillion paired lists, each pair of equal size. I want to "merge" each by picking a random element from each index, but my current implementation is very slow - even when multiprocessing. (FWIW, my code does need to be threadable).

def rand_merge(l1, l2):
    newl = []
    for i in range(len(l1)):
        q = random.choice([l1, l2])
        newl.append(q[i])
    return newl

Pretty basic, but running it on 20k lists of sizes ~5-25, it takes crazy long - I assume it's random.choice gumming up the works. But I've also tried other versions of random, like creating a string of 0's and 1's to refer to, no go.




Aucun commentaire:

Enregistrer un commentaire