samedi 5 décembre 2015

Please explain Python random.choice population code

I am working on a small python application and found this piece of code in the Python 3.4 random documentation: http://ift.tt/1jGLNpd

 weighted_choices = [('Red', 3), ('Blue', 2), ('Yellow', 1), ('Green', 4)]
 population = [val for val, cnt in weighted_choices for i in range(cnt)]
 random.choice(population)

It works perfectly; however, I don't fully understand what's going on in the second line.

I'm asking solely because I want to broaden my understanding of Python.




Aucun commentaire:

Enregistrer un commentaire