Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: arrays in python

Reply
Thread Tools

Re: arrays in python

 
 
Simon Forman
Guest
Posts: n/a
 
      09-23-2009
On Wed, Sep 23, 2009 at 1:14 PM, Rudolf <> wrote:
> Can someone tell me how to allocate single and multidimensional arrays
> in python. I looked online and it says to do the following x =
> ['1','2','3','4']
>
> However, I want a much larger array like a 100 elements, so I cant
> possibly do that. I want to allocate an array and then populate it
> using a for loop. Thanks for your help.
> --
> http://mail.python.org/mailman/listinfo/python-list
>


In python they're called 'lists'. There are C-style array objects but
you don't want to use them unless you specifically have to.

You can create an empty list like so:

x = []

and then put items into it with a for loop like so:

for item in some_iterable:
x.append(item)

But in simple cases like this there's also the "list comprehension"
syntax which will do it all in one step:

x = [item for item in some_iterable]

But again in a case like this, if you're simply populating a list from
an iterable source you would just say:

x = list(some_iterable)

For multidimensional 'arrays' you just put lists in lists. (Or use
NumPy if you really want arrays and are doing lots of wild processing
on them.)

But if you do this:

two_dimensional_list = [ [] for var in some_iterable]

The list of lists you create thereby will contain multiple references
to the /same/ inner list object. This is something that catches many
people unawares. You have to go back to using a for loop:

x = []
for n in range(100):
x.append([(n, m) for m in range(10)])

(That will create a list of one hundred lists, each of which contains
ten tuples.)

Good luck.
 
Reply With Quote
 
 
 
 
AggieDan04
Guest
Posts: n/a
 
      09-24-2009
On Sep 23, 3:02*pm, Simon Forman <sajmik...@gmail.com> wrote:
> On Wed, Sep 23, 2009 at 1:14 PM, Rudolf <yellowblueyel...@gmail.com> wrote:
> > Can someone tell me how to allocate single and multidimensional arrays
> > in python. I looked online and it says to do the following x =
> > ['1','2','3','4']

>
> > However, I want a much larger array like a 100 elements, so I cant
> > possibly do that. I want to allocate an array and then populate it
> > using a for loop. Thanks for your help.
> > --
> >http://mail.python.org/mailman/listinfo/python-list

>
> In python they're called 'lists'. *There are C-style array objects but
> you don't want to use them unless you specifically have to.

....
> But if you do this:
>
> two_dimensional_list = [ [] for var in some_iterable]
>
> The list of lists you create thereby will contain multiple references
> to the /same/ inner list object.


No, that creates a list of distinct empty lists. If you want multiple
references to the same inner list, it's

inner_list = []
two_dimensional_list = [inner_list for var in some_iterable]

or

two_dimensional_list = [[]] * num_copies
 
Reply With Quote
 
 
 
 
Simon Forman
Guest
Posts: n/a
 
      09-24-2009
On Wed, Sep 23, 2009 at 10:03 PM, AggieDan04 <> wrote:
> On Sep 23, 3:02*pm, Simon Forman <sajmik...@gmail.com> wrote:
>> On Wed, Sep 23, 2009 at 1:14 PM, Rudolf <yellowblueyel...@gmail.com> wrote:
>> > Can someone tell me how to allocate single and multidimensional arrays
>> > in python. I looked online and it says to do the following x =
>> > ['1','2','3','4']

>>
>> > However, I want a much larger array like a 100 elements, so I cant
>> > possibly do that. I want to allocate an array and then populate it
>> > using a for loop. Thanks for your help.
>> > --
>> >http://mail.python.org/mailman/listinfo/python-list

>>
>> In python they're called 'lists'. *There are C-style array objects but
>> you don't want to use them unless you specifically have to.

> ...
>> But if you do this:
>>
>> two_dimensional_list = [ [] for var in some_iterable]
>>
>> The list of lists you create thereby will contain multiple references
>> to the /same/ inner list object.

>
> No, that creates a list of distinct empty lists. *If you want multiple
> references to the same inner list, it's
>
> inner_list = []
> two_dimensional_list = [inner_list for var in some_iterable]
>
> or
>
> two_dimensional_list = [[]] * num_copies
> --
> http://mail.python.org/mailman/listinfo/python-list
>


Oh, you're right. I was thinking of the [[]] * n form. My bad.

~Simon
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
numpy arrays to python compatible arrays Javier Montoya Python 3 06-12-2010 09:32 AM
Multidimensional arrays and arrays of arrays Philipp Java 21 01-20-2009 08:33 AM
char arrays and integer arrays... why the difference? Bill Reyn C++ 3 06-22-2004 12:01 PM
Arrays.asList() returning java.util.Arrays$ArrayList Alexandra Stehman Java 5 06-17-2004 06:04 PM
initializing arrays of arrays Mantorok Redgormor C Programming 4 09-11-2003 02:08 AM



Advertisments