Smarter way of doing this?

Max M maxm at mxm.dk
Tue Feb 3 12:52:54 CET 2004


Roberto A. F. De Almeida wrote:

> In one line:
> 
> v = ['item 1','item 2','item 3',]
> p = [0.66, 0.33, 0.16]
> 
> [v[i] for i,j in enumerate(p) if sum(p[:i]) > (random.random()*sum(p))][0]


uh, that's hard to read :-)

But doesn't random.random()*sum(p) get executed for each probability in p?

Then each probability in p will be compared to a different random value. 
That isn't fair...


regards Max m



More information about the Python-list mailing list