Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > How to make arrays from Lists

Reply
Thread Tools

How to make arrays from Lists

 
 
gc_ottawa@yahoo.ca
Guest
Posts: n/a
 
      11-12-2008
I want to construct a 2-dimensional array from a List but I cannot
find a simple way of changing any element. For example, construct a
3x3 array like this:-
>>> x=[0,0,0]

x=[x]*3
this produces [[0,0,0],[0,0,0],[0,0,0]. So far so good.
How do I change the value of any element to produce (say)
[[99,0,0],[0,0,0],[0,0,0]] ?

gordc
 
Reply With Quote
 
 
 
 
bearophileHUGS@lycos.com
Guest
Posts: n/a
 
      11-12-2008
gc_ott:
> How do I change the value of any element to produce (say)
> [[99,0,0],[0,0,0],[0,0,0]] ?
>
> gordc


To create a 2D list, that is a list of lists:
x = [[0] * ncols for i in nrows]
(Don't do what you were doing, because you end with many references to
the same list, and that will give you troubles.)

To set an item you do just:
x[0][0] = 99

Bye,
bearophile
 
Reply With Quote
 
 
 
 
gc_ottawa@yahoo.ca
Guest
Posts: n/a
 
      11-12-2008
On Nov 11, 7:48*pm, (E-Mail Removed) wrote:
> gc_ott:
>
> > How do I change the value of any element to produce (say)
> > [[99,0,0],[0,0,0],[0,0,0]] ?

>
> > gordc

>
> To create a 2D list, that is a list of lists:
> x = [[0] * ncols for i in nrows]
> (Don't do what you were doing, because you end with many references to
> the same list, and that will give you troubles.)
>
> To set an item you do just:
> x[0][0] = 99
>
> Bye,
> bearophile


Many thanks, I don't think I would ever 'discovered' this.
gordc
 
Reply With Quote
 
Robert Kern
Guest
Posts: n/a
 
      11-12-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> On Nov 11, 7:48 pm, (E-Mail Removed) wrote:
>> gc_ott:
>>
>>> How do I change the value of any element to produce (say)
>>> [[99,0,0],[0,0,0],[0,0,0]] ?
>>> gordc

>> To create a 2D list, that is a list of lists:
>> x = [[0] * ncols for i in nrows]
>> (Don't do what you were doing, because you end with many references to
>> the same list, and that will give you troubles.)
>>
>> To set an item you do just:
>> x[0][0] = 99
>>
>> Bye,
>> bearophile

>
> Many thanks, I don't think I would ever 'discovered' this.


If you are trying to emulate numerical arrays, you may want to use numpy, instead.

http://numpy.scipy.org/

import numpy
x = numpy.zeros([3,3], dtype=int)
x[0,0] = 99

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

 
Reply With Quote
 
Gabriel Genellina
Guest
Posts: n/a
 
      11-15-2008
En Tue, 11 Nov 2008 23:09:25 -0200, <(E-Mail Removed)> escribió:
> On Nov 11, 7:48*pm, (E-Mail Removed) wrote:


>> To create a 2D list, that is a list of lists:
>> x = [[0] * ncols for i in nrows]

>
> Many thanks, I don't think I would ever 'discovered' this.


Try reading the FAQ:
http://www.python.org/doc/faq/progra...mensional-list

--
Gabriel Genellina

 
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
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Multidimensional arrays and arrays of arrays Philipp Java 21 01-20-2009 08:33 AM
List of lists of lists of lists... =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==?= Python 5 05-15-2006 11:47 AM
lists of lists Jon Slaughter C++ 4 12-13-2004 06:28 PM
Sorting lists of lists... JustSomeGuy C++ 0 06-17-2004 05:44 PM



Advertisments