1

I am working on a small python application and found this piece of code in the Python 3.4 random documentation: https://docs.python.org/3/library/random.html?highlight=random#module-random

 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.

E.Arrowood
  • 750
  • 7
  • 17
  • I guess I wasn't clear in my first post. What I really want to know is how the weighting of each value works. BUT I think I figured it out by looking at the answers on the other post. Essentially, it adds the value, the number of times specified. – E.Arrowood Dec 06 '15 at 03:58

0 Answers0