random playing soundfiles according to rating.
Ben Cartwright
bencvt at gmail.com
Fri Feb 10 13:37:26 EST 2006
kpp9c wrote:
> I've been looking at some of the suggested approaches and looked a
> little at Michael's bit which works well.... bisect is a module i
> always struggle with (hee hee)
>
> I am intrigued by Ben's solution and Ben's distilled my problem quite
> nicely
Thanks!-) Actually, you should use Michael's solution, not mine. It
uses the same concept, but it finds the correct subinterval in O(log n)
steps (by using bisect on a cached list of cumulative sums). My code
takes O(n) steps -- this is a big difference when you're dealing with
thousands of items.
> but, well....i don't understand what "point" is doing with
> wieght for key, weight for zlist
This line:
point = random.uniform(0, sum(weight for key, weight in zlist))
Is shorthand for:
total = 0
for key, weight in zlist:
total += weight
point = random.uniform(0, total)
> furthermore, it barfs in my
> interpreter... (Python 2.3)
Oops, that's because it uses generator expressions
(http://www.python.org/peps/pep-0289.html), a 2.4 feature. Try
rewriting it longhand (see above). The second line of the test code
will have to be changed too, i.e.:
>>> counts = dict([(key, 0) for key, weight in data])
--Ben
More information about the Python-list
mailing list