How do you do arrays

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

    #16
    Re: How do you do arrays

    Thanks
    Tom
    In article <-rWdnWyelepdn53f RVn-oQ@rogers.com>, "Dan Perl"
    <danperl@mail-me.com> wrote:
    [color=blue]
    > A solution that I haven't seen mentioned by other postings in the thread is
    > to implement the array as a dictionary:
    >
    > iMatrix = {}
    > for index in range(majorlop1 ):
    > k = random.choice(l istvalues) + 1
    > iMatrix[index] = k
    >
    > Mind you, a dictionary does not behave *exactly* like an array. For
    > instance, in your example, you may later do a "del iMatrix[2]" and then you
    > wouldn't really be able to use iMatrix like an array anymore. But,
    > depending on your application, a dictionary may be perfectly suitable.
    >
    > Hope this helps.
    >
    > Dan
    >
    > "Thomas Bunce" <tbunce@mac.com > wrote in message
    > news:tbunce-010205105245000 1@192.168.1.12. ..[color=green]
    > >I am new at Pyton and I am learning from book not classes
    > > so please forgive my being slow
    > >
    > > The below does not work I get an Error of File
    > > "Matrix[index] = k
    > > NameError: name 'iMatrix' is not defined"
    > >
    > > while index < majorlop1:
    > > index = index + 1
    > > k = random.choice(l istvalues) + 1
    > > iMatrix[index] = k
    > >
    > > The book statement of
    > > array(typecode, initializer) does not make sence
    > > to me how it henerates ore relaes to the org name
    > > for the array.
    > >
    > > Thank You
    > > Tom[/color][/color]

    Comment

    • Dennis Lee Bieber

      #17
      Re: How do you do arrays

      On 1 Feb 2005 13:03:12 -0800, beliavsky@aol.c om declaimed the following
      in comp.lang.pytho n:

      [color=blue]
      > I think most versions of Basic have arrays that by default start with
      > 1, as did their ancestor, Fortran, although other lower bounds can be[/color]

      Classic BASIC actually splits the difference.

      dim a(10)

      allocates 11 elements, indexed 0..10 -- but most classes tend to ignore
      element 0, and algorithms are as if only 10 elements exist starting at
      1.

      --[color=blue]
      > =============== =============== =============== =============== == <
      > wlfraed@ix.netc om.com | Wulfraed Dennis Lee Bieber KD6MOG <
      > wulfraed@dm.net | Bestiaria Support Staff <
      > =============== =============== =============== =============== == <
      > Home Page: <http://www.dm.net/~wulfraed/> <
      > Overflow Page: <http://wlfraed.home.ne tcom.com/> <[/color]

      Comment

      • Erik Max Francis

        #18
        Re: How do you do arrays

        Dennis Lee Bieber wrote:
        [color=blue]
        > Classic BASIC actually splits the difference.
        >
        > dim a(10)
        >
        > allocates 11 elements, indexed 0..10 -- but most classes tend to ignore
        > element 0, and algorithms are as if only 10 elements exist starting at
        > 1.[/color]

        Basic also has the OPTION BASE instruction, which affects that.

        --
        Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
        San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
        War is like love, it always finds a way.
        -- Bertolt Brecht

        Comment

        • Alex Martelli

          #19
          Re: How do you do arrays

          Kartic <kartic.krishna murthy@gmail.co m> wrote:
          ...[color=blue]
          > I am not sure what book you are using but I don't think it is a very
          > good one.[/color]

          Hmmm, considering he said it's "Python in a Nutshell", I disagree with
          you;-). If he had understood that he probably wanted to use lists, not
          arrays, the top paragraph on p. 47 might have helped him -- "Assigning
          to an item with an invalid index also raises an exception", and list
          comprehensions are on p. 56.

          But considering that in the first post he was just trying to assign to
          an indexing on a name he had never bound at all, it seems to me that
          starting with the Nutshell may simply have been a bit of "running before
          you can walk". The Nutshell does mention that names (variables) spring
          into existence when you bind them, which implies they don't exist
          previously, and thus you can't perform any operation on them (such as
          binding an indexing on them) before you're bound them; but the Nutshell
          doesn't extensively belabor the point.

          I tried to explain this issue to another poster who recently appeared to
          want to turn the Nutshell into a lay version of the Heart Sutra
          ("Therefore, Shariputra, in Python there are no declarations. There is
          no use strict, no option explicit, no implicit none; no type of a name
          and no declaring of a name so that it can't be rebound, no defining of a
          name's type and no omitting to define a name's type, ..."). In a quick
          reference, there just isn't enough space to repeat every key point, much
          less to belabor the implications of each; even if space were free,
          having to skip through such repetitions would _waste_ time for the main
          target audience -- people who know some Python and want to get reminded
          of every detail about a certain subject, or just look up something
          specific. Trying to make a book that's both a hand-holding tutorial
          _and_ a quick reference would produce a book that is not very good at
          either task, IMHO.


          Alex

          Comment

          • Dennis Lee Bieber

            #20
            Re: How do you do arrays

            On Tue, 01 Feb 2005 23:12:57 -0800, Erik Max Francis <max@alcyone.co m>
            declaimed the following in comp.lang.pytho n:
            [color=blue]
            >
            > Basic also has the OPTION BASE instruction, which affects that.[/color]

            I did say "classic".. . Option Base came in with, what, GW-BASIC?

            --[color=blue]
            > =============== =============== =============== =============== == <
            > wlfraed@ix.netc om.com | Wulfraed Dennis Lee Bieber KD6MOG <
            > wulfraed@dm.net | Bestiaria Support Staff <
            > =============== =============== =============== =============== == <
            > Home Page: <http://www.dm.net/~wulfraed/> <
            > Overflow Page: <http://wlfraed.home.ne tcom.com/> <[/color]

            Comment

            Working...