On Fri, 19 Dec 2014 10:32:15 +0100
Peter Otten <__pete...@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()

_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to