How to initialize an array with a large number of members ?

MRAB google at mrabarnett.plus.com
Wed Dec 31 11:57:59 EST 2008


David at bag.python.org wrote:
> Thanks to those who have helped a beginner in
> Python. Using version 3.0
> 
> # script23
> from array import array
>     < see failed initialization attempts  below >
> 
>     tally = array('H',for i in range(75) : [0])
> 
>     tally = array('H',[for i in range(75) : 0])
> 
>     tally = array('H',range(75) : [0])
> 
>     tally = array('H',range(75) [0])
> 
>           Above give either Syntax error or TypeError
> 
>        All examples of sequences in docs show only
>        a few members being initialized. Array doc 
>        says an iterator can be used, but doen't show
>        how.  What would you do for a 2 dimensional 
>        array ?  Incorporate c code ?
>        The online docs are clearly insufficient for a
>        novice.  Have ordered Summerfields new book.
> 
 >>> # With a list
 >>> tally = array('H', [0] * 75)
 >>>
 >>> # With a generator
 >>> tally = array('H', (0 for i in range(75)))



More information about the Python-list mailing list