Re: Most efficient way to "pre-grow" a list?
by Gil_johnson other posts by this author
Nov 6 2009 6:50PM messages near this date
Re: Most efficient way to "pre-grow" a list?
|
Re: Most efficient way to "pre-grow" a list?
On Nov 6, 6:12Â am, kj <no.em...@[...].post> wrote:
> In Perl one can assign a value to any element of an array, even to
> ones corresponding to indices greater or equal than the length of
> the array:
>
> Â my @arr;
> Â $arr[999] = 42;
>
> perl grows the array as needed to accommodate this assignment. Â In
> fact one common optimization in Perl is to "pre-grow" the array to
> its final size, rather than having perl grow it piecemeal as required
> by assignments like the one above:
>
> Â my @arr;
> Â $#arr = 999_999;
>
> After assigning to $#arr (the last index of @arr) as shown above,
> @arr has length 1,000,000, and all its elements are initialized to
> undef.
>
> In Python the most literal translation of the first code snippet
> above triggers an IndexError exception:
>
> >>> arr = list()
> >>> arr[999] = 42
>
> Traceback (most recent call last):
> Â File "<stdin>", line 1, in <module>
> IndexError: list assignment index out of range
>
> In fact, one would need to pre-grow the list sufficiently to be
> able to make an assignment like this one. Â I.e. one needs the
> equivalent of the second Perl snippet above.
>
> The best I can come up with is this:
>
> arr = [None] * 1000000
>
> Is this the most efficient way to achieve this result?
>
> TIA!
>
> kynn
I don't have the code with me, but for huge arrays, I have used
something like:
> >> arr[0] = initializer
> >> for i in range N:
> >> arr.extend(arr)
This doubles the array every time through the loop, and you can add
the powers of 2 to get the desired result.
Gil
--
http://mail.python.org/mailman/listinfo/python-list
Thread:
Kj
Andre Engels
Steven D'Aprano
Francis Carr
Gil_johnson
Gabriel Genellina
Gb345
Gil_johnson
Gil_johnson
Paul Rudin
Carl Banks
Emile van Sebille
exarkun
Sturlamolden
Sturlamolden
Sturlamolden
Kj
Ivan Illarionov
Steven D'Aprano
Terry Reedy
R
Gil_johnson
Kj
Bruno Desthuilliers
Luis Alberto Zarrabeitia Gomez
Andre Engels
Luis Alberto Zarrabeitia Gomez
Emily Rodgers
Raymond Hettinger
Brian Curtin
Paul Rubin
Jon Clements
|