Another question

jimsp jimsp at ichips.intel.com
Mon Apr 17 16:37:36 EDT 2000


How about

>>> import random
>>> x = [0]*1000000
>>> y = map(lambda x: random.randint(1,10), x)
>>> len(x)
1000000
>>> len(y)
1000000
>>>
>>>
>>> x[1:100]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0
, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0,
 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
>>> y[1:100]
[6, 5, 6, 8, 2, 4, 10, 7, 10, 5, 6, 8, 4, 3, 9, 10, 7, 9, 9, 5, 6, 7, 1, 3, 7,
2
, 7, 4, 7, 2, 6, 3, 9, 5, 4, 9, 2, 7, 2, 6, 5, 8, 7, 8, 1, 6, 10, 4, 3, 7, 3,
3,
 4, 8, 5, 1, 5, 7, 5, 9, 9, 5, 2, 3, 1, 8, 3, 5, 3, 4, 2, 9, 1, 6, 8, 8, 4, 6,
4
, 7, 2, 8, 5, 7, 2, 7, 9, 10, 7, 4, 8, 10, 6, 9, 4, 3, 3, 10, 8]
>>>

--james



Donald Beaudry wrote:

> Bill Scherer <scherbi at bam.com> wrote,
> > I just did a little test and found this to be substantially fatser than the
> > other three:
> >
> > import random
> > randint = random.randint
> >
> > r = []
> >
> > for i in xrange(1,10000):
> >     r.append(randint(1,10))
> >
> > for i in r: print i
> > #
>
> ...then this one should be really impressive ;)
>
>   def do_it():
>       from random import randint
>       r = []
>       append = r.append
>       for i in xrange(1,10000):
>          append(randint(i))
>       for i in r:
>          print i
>
>   do_it()
>
> or better yet:
>
>   def do_it():
>       from random import randint
>       r = []
>       append = r.append
>       map(append, xrange(1,10000))
>       for i in r:
>          print i
>
>   do_it()
>
> --
> Donald Beaudry                                     Ab Initio Software Corp.
>                                                    201 Spring Street
> donb at init.com                                      Lexington, MA 02421
>                   ...So much code, so little time...




More information about the Python-list mailing list