[Tutor] lottery problem (Was Re: (no subject))
Adam Jensen
hanzer at riseup.net
Fri Dec 19 17:57:28 CET 2014
On Fri, 19 Dec 2014 10:32:15 +0100
Peter Otten <__peter__ at web.de> wrote:
> Basically
>
> from random import randint, seed
>
> is equivalent to
>
> import random
> randint = random.randint
> seed = random.seed
> del random
>
> From that you can deduce that the whole random module is loaded into memory
> in both cases. A small speed advantage may be caused when the attribute
> lookup is avoided in a tight loop
Thanks for the clarification, that's really helpful. So I guess the import style is more about name space management than controlling the details of what gets loaded...
> or -- if you go back to the original problem -- with some effort:
>
> >>> N = 3
> >>> numpy.random.randint(1, 10, N) + numpy.arange(0, N*10, 10)
> array([ 5, 11, 27])
>
> In return the latter is likely significantly more efficient for large N than
> the generic list comprehension.
Ha! That's fun. Seven seems to be a magic number in the original problem, so maybe a little tweak:
import numpy as np
N=7
(np.random.randint(1,N,N) + np.arange(0,N*N,N)).tolist()
More information about the Tutor
mailing list