lists <-> tuple

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

    #1

    lists <-> tuple

    I am new to Python and have to create an import library in C that uses
    matrices.

    These matrices can be one-dimensional (vectors) or two-dimensional. If I
    look in the ActivePython 2.4 documentation at data structures, then I see at
    least 2 posibilities to represent them: Lists and Tuples.

    The documention doesn't give me much information on what the best choice is
    for the data type to provide/return these matrices.

    So my question is, should I use lists or tuples to represent my matrices in
    and why?

    Thanks for any reaction.


  • Jim

    #2
    Re: lists &lt;-&gt; tuple

    > Tuples or lists for matrix-like functionality?

    Use lists. Tuples are meant for small immutable sets of things that go
    together. Lists are more like arrays, and you can assign to one
    existing element if you want.

    One exception, is a short vector is often a tuple like (x, y, z) and
    you might want to multiply that vector by your matrix. You can convert
    a tuple to a list with list(aTuple) or back with tuple(aList.)

    Even better, take a look at numarray (or numpy or scipy or scipy_core.)
    They all have really nice matrix code and there are C APIs that let
    you manipulate them. Chances are they do everything you're intending
    to implement.

    Immutability example:
    tup = ("a", "b", "c")
    tup[1] = "g"
    Traceback (most recent call last):
    File "<input>", line 1, in ?
    TypeError: object does not support item assignment
    lst = ["a", "b", "c"]
    lst[1] = "g"
    lst
    ['a', 'g', 'c']


    -Jim

    Comment

    • Robert Kern

      #3
      Re: lists &lt;-&gt; tuple

      Peter Notebaert wrote:[color=blue]
      > I am new to Python and have to create an import library in C that uses
      > matrices.
      >
      > These matrices can be one-dimensional (vectors) or two-dimensional. If I
      > look in the ActivePython 2.4 documentation at data structures, then I see at
      > least 2 posibilities to represent them: Lists and Tuples.
      >
      > The documention doesn't give me much information on what the best choice is
      > for the data type to provide/return these matrices.
      >
      > So my question is, should I use lists or tuples to represent my matrices in
      > and why?[/color]

      You'll probably want to use scipy_core. It's a package designed
      specifically to deal with multidimensiona l arrays of homogeneous,
      (usually) numeric data.



      --
      Robert Kern
      rkern@ucsd.edu

      "In the fields of hell where the grass grows high
      Are the graves of dreams allowed to die."
      -- Richard Harter

      Comment

      • Peter Notebaert

        #4
        Re: lists &lt;-&gt; tuple


        "Jim" <mrmaple@gmail. com> wrote in message
        news:1130973892 .137886.170410@ f14g2000cwb.goo glegroups.com.. .[color=blue][color=green]
        >> Tuples or lists for matrix-like functionality?[/color]
        >
        > Use lists. Tuples are meant for small immutable sets of things that go
        > together. Lists are more like arrays, and you can assign to one
        > existing element if you want.
        >
        > One exception, is a short vector is often a tuple like (x, y, z) and
        > you might want to multiply that vector by your matrix. You can convert
        > a tuple to a list with list(aTuple) or back with tuple(aList.)
        >
        > Even better, take a look at numarray (or numpy or scipy or scipy_core.)
        > They all have really nice matrix code and there are C APIs that let
        > you manipulate them. Chances are they do everything you're intending
        > to implement.
        >
        > Immutability example:
        > tup = ("a", "b", "c")
        > tup[1] = "g"
        > Traceback (most recent call last):
        > File "<input>", line 1, in ?
        > TypeError: object does not support item assignment
        > lst = ["a", "b", "c"]
        > lst[1] = "g"
        > lst
        > ['a', 'g', 'c']
        >
        >
        > -Jim
        >[/color]

        Thanks!


        Comment

        • Peter Notebaert

          #5
          Re: lists &lt;-&gt; tuple


          "Robert Kern" <robert.kern@gm ail.com> wrote in message
          news:mailman.47 .1130974709.187 01.python-list@python.org ...[color=blue]
          > Peter Notebaert wrote:[color=green]
          >> I am new to Python and have to create an import library in C that uses
          >> matrices.
          >>
          >> These matrices can be one-dimensional (vectors) or two-dimensional. If I
          >> look in the ActivePython 2.4 documentation at data structures, then I see
          >> at
          >> least 2 posibilities to represent them: Lists and Tuples.
          >>
          >> The documention doesn't give me much information on what the best choice
          >> is
          >> for the data type to provide/return these matrices.
          >>
          >> So my question is, should I use lists or tuples to represent my matrices
          >> in
          >> and why?[/color]
          >
          > You'll probably want to use scipy_core. It's a package designed
          > specifically to deal with multidimensiona l arrays of homogeneous,
          > (usually) numeric data.
          >
          > http://numeric.scipy.org
          >
          > --
          > Robert Kern
          > rkern@ucsd.edu
          >
          > "In the fields of hell where the grass grows high
          > Are the graves of dreams allowed to die."
          > -- Richard Harter
          >[/color]

          Thanks!


          Comment

          Working...