python list/array question...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • bruce

    #1

    python list/array question...

    hi...

    i'm trying to deal with multi-dimension lists/arrays

    i'd like to define a multi-dimension string list, and then manipulate the
    list as i need... primarily to add lists/information to the 'list/array' and
    to compare the existing list information to new lists

    i'm not sure if i need to import modules, or if the base python install i
    have is sufficient. an example, or pointer to examples would be good...

    i'd like

    define a[][]

    #basically, i'd like a 3x3 array, where each element
    #has one of the a,b,c items..
    # |a1, b1, c1|
    # |a2, b2, c2|
    # |a3, b3, c3|

    a[1][1] = ['a1','b1','c1']
    a[1][2] = ['a2','b2','c2']
    a[1][3] = ['a3','b3','c3']

    b = ['f','g','h']
    v = ['f1','g1','h1']

    if a[1][2] == b
    print 'good!'

    a[1][4] = b

    x = 4
    g = ['p1','l1','g1']

    for i in range[g]
    a[x][i] = g[i]


    these are the kinds of list/array functions i'd like to be able to
    accomplish

    pointers/code samples/pointers to code would be helpful...

    and yeah. i've been looking via google...

    thanks

    -bruce



  • Bruno Desthuilliers

    #2
    Re: python list/array question...

    bruce wrote:
    hi...
    >
    i'm trying to deal with multi-dimension lists/arrays
    Python has lists (which AFAIK really are arrays not linked lists, but
    they are called 'lists'). FWIW, this is in the fine manual.
    i'd like to define a multi-dimension string list, and then manipulate the
    list as i need... primarily to add lists/information to the 'list/array' and
    to compare the existing list information to new lists
    >
    i'm not sure if i need to import modules, or if the base python install i
    have is sufficient.
    ?????

    importing modules doesn't require installing additional packages (unless
    the modules you want to import are not part of the stdlib nor of your
    application).
    an example, or pointer to examples would be good...

    i'd like
    >
    define a[][]
    No "define" statement in Python - as you would know if you had read the
    fine manual.
    #basically, i'd like a 3x3 array, where each element
    #has one of the a,b,c items..
    # |a1, b1, c1|
    # |a2, b2, c2|
    # |a3, b3, c3|
    >
    a[1][1] = ['a1','b1','c1']
    Python's list are zero-based (which is the common case). This is
    mentionned in the fine manual.
    a[1][2] = ['a2','b2','c2']
    a[1][3] = ['a3','b3','c3']
    a = [
    ['a1','b1','c1'],
    ['a2','b2','c2'],
    ['a3','b3','c3'],
    ]

    or

    a = []
    a.append(['a1','b1','c1'])
    a.append(['a2','b2','c2'])
    a.append(['a3','b3','c3'])

    (etc - cf the fine manual).
    b = ['f','g','h']
    v = ['f1','g1','h1']
    >
    if a[1][2] == b
    print 'good!'
    >
    a[1][4] = b
    >
    x = 4
    g = ['p1','l1','g1']
    >
    for i in range[g]
    a[x][i] = g[i]
    >
    >
    these are the kinds of list/array functions i'd like to be able to
    accomplish
    >
    pointers/code samples/pointers to code would be helpful...
    start here : http://www.python.org/doc/
    and yeah. i've been looking via google...
    Really ?



    --
    bruno desthuilliers
    python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
    p in 'onurb@xiludom. gro'.split('@')])"

    Comment

    • Diez B. Roggisch

      #3
      Re: python list/array question...

      Sybren Stuvel wrote:
      Bruno Desthuilliers enlightened us with:
      >Python has lists (which AFAIK really are arrays not linked lists,
      >but they are called 'lists').
      >
      An array is generally understood as a list of items of the same type,
      hence Python lists aren't arrays.
      Only in the same sense as lists are. Inhomogeneous lists are of course
      possible in python, and maybe even common, but many people argue that is
      bad style. And in FP languages they are especially frown upon.

      And AFAIK the internal representation is an array of object-pointers.

      Regards,

      Diez

      Comment

      • Alex Martelli

        #4
        Re: python list/array question...

        Sybren Stuvel <sybrenUSE@YOUR thirdtower.com. imaginationwrot e:
        Bruno Desthuilliers enlightened us with:
        Python has lists (which AFAIK really are arrays not linked lists,
        but they are called 'lists').
        >
        An array is generally understood as a list of items of the same type,
        hence Python lists aren't arrays.
        Hmmm...
        >>x = Numeric.array([23, 4.5, 'zap!'], 'O')
        >>type(x)
        <type 'array'>
        >>x
        array([23 , 4.5 , zap! ],'O')

        Should I think that an array (Numeric.array) is not an array? After all
        it can hold just the same variety of item types as a Python list. Or is
        it more useful to say that the "type" (which all the items have in
        common) is "Python object"? (After all, 'object' IS what the typecode
        letter 'O' stands for). I definitely like to think of Numeric.array's
        as arrays, and I think it's both proper and useful to do so...


        Alex

        Comment

        • Bruno Desthuilliers

          #5
          Re: python list/array question...

          Sybren Stuvel wrote:
          Bruno Desthuilliers enlightened us with:
          >
          >>Python has lists (which AFAIK really are arrays not linked lists,
          >>but they are called 'lists').
          >
          >
          An array is generally understood as a list of items of the same type,
          hence Python lists aren't arrays.
          A list is generally understood as a linked list, hence Python lists are
          not lists !-)

          Also, in statically typed languages, lists are supposed to be homogenous
          ordered collections of variable length (cf ML/Haskell...). And FWIW,
          this is also the intended semantic for Python lists (cf GvR's comments
          on list vs tuple respective semantics)

          But, to be honnest:
          in a lot of languages, an array is supposed to be of fixed size, hence
          Python lists aren't arrays !-)

          Now the question is: since Python lists are neither arrays nor lists,
          how should we name them ?-)

          --
          bruno desthuilliers
          python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
          p in 'onurb@xiludom. gro'.split('@')])"

          Comment

          • Vic.Kelson@gmail.com

            #6
            Re: python list/array question...

            Another way to do it is using a dict with keys that are tuples:
            >>arr = {}
            >>arr[0,0] = 'a1'
            >>arr[0,1] = 'a2'
            >>arr[1,0] = 'b1'
            >>arr[1,1] = 'b2'
            >>arr[2,0] = 'c1'
            >>arr[2,1] = 'c2'
            >>for j in range(3):
            .... for i in range(2):
            .... print arr[j,i], ' ',
            .... print
            ....
            a1 a2
            b1 b2
            c1 c2
            >>>
            You can derive a class from dict that implements desired behaviors
            (e.g. bounds checking, nrows and ncols attributes). Using this approach
            over lists of lists is nice: old fogey scientific programmers like me
            prefer the explicit [j,i] syntax over the [j][i] way, and it's easily
            extensible over more than two dimensions ([k][j][i]??? Yikes!). This is
            perhaps not preferred for a "full" matrix (I'd tend to use a NumPy
            array of Objects), but it can be useful.

            I've used it in two cases:

            -- A "sparse" matrix (overload dict.__getitem_ _ to return 0 if the
            tuple (j,i) is not a valid key),

            -- A "multidimension al array" in which all the indices are strings,
            like a table with column labels and row labels (in my case, it was a
            3-dimensional "table" with a concatenated key of three text strings but
            there were reasons not to use a relational database). It was very
            convenient to implement the code in this way, and extremely readable.

            This trick is also useful with dbm files, e.g. using shelve.

            --vic

            Comment

            Working...