How to make arrays from Lists

Discussion in 'Python' started by gc_ottawa@yahoo.ca, Nov 12, 2008.

  1. Guest

    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
    , Nov 12, 2008
    #1
    1. Advertising

  2. Guest

    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
    , Nov 12, 2008
    #2
    1. Advertising

  3. Guest

    On Nov 11, 7:48 pm, 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
    , Nov 12, 2008
    #3
  4. Robert Kern Guest

    wrote:
    > On Nov 11, 7:48 pm, 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
    Robert Kern, Nov 12, 2008
    #4
  5. Gabriel Genellina, Nov 15, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JustSomeGuy

    Sorting lists of lists...

    JustSomeGuy, Jun 17, 2004, in forum: C++
    Replies:
    0
    Views:
    321
    JustSomeGuy
    Jun 17, 2004
  2. Jon Slaughter

    lists of lists

    Jon Slaughter, Dec 13, 2004, in forum: C++
    Replies:
    4
    Views:
    421
    Buster
    Dec 13, 2004
  3. Charlotte Henkle

    Counter for items in lists in lists?

    Charlotte Henkle, Sep 25, 2004, in forum: Python
    Replies:
    8
    Views:
    400
    Charlotte Henkle
    Sep 26, 2004
  4. =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==

    List of lists of lists of lists...

    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==, May 8, 2006, in forum: Python
    Replies:
    5
    Views:
    404
    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==
    May 15, 2006
  5. Philipp
    Replies:
    21
    Views:
    1,127
    Philipp
    Jan 20, 2009
Loading...

Share This Page