Speeding up the implementation of Stochastic Gradient Ascent in Python

Ned Batchelder ned at nedbatchelder.com
Wed Jan 17 15:19:04 EST 2018


On 1/17/18 2:45 PM, Chris Angelico wrote:
> On Thu, Jan 18, 2018 at 6:28 AM, Ned Batchelder <ned at nedbatchelder.com> wrote:
>> You'll have to replace random.choice() with
>> random.choice(list(...)), since you can't random.choice from a set.
>>
> Side point: why can't you? You can random.sample from a set, but
> random.choice requires a sequence. It seems perfectly sane to ask for
> a random element from a set.
>
>

You'd have to ask Raymond Hettinger I guess.  random.sample works on a 
set because it starts with:

         if isinstance(population, _Set):
             population = tuple(population)

So sample() is willing to listify (tuplify?) your set for you, but 
choice() is not.

--Ned.



More information about the Python-list mailing list