How do you do arrays

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

    #1

    How do you do arrays

    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
  • Kartic

    #2
    Re: How do you do arrays

    Tom,

    Before you use iMatrix[index], you have to tell python to use iMatrix
    as an array. You will do that using iMatrix = [] *outside* the loop.

    iMatrix = []
    while index < majorlop1: # rest of the loop statements

    Since you are new, please take a look at the Python tutorial to get you
    started.
    Python is an easy to learn, powerful programming language. It has efficient high-level data structures and a simple but effective approach to object-oriented programming. Python’s elegant syntax an...


    Thanks,
    -Kartic

    Comment

    • Kartic

      #3
      Re: How do you do arrays

      and it is called a "List" in Python parlance.

      Comment

      • Sean Blakey

        #4
        Re: How do you do arrays

        On Tue, 01 Feb 2005 10:52:45 -0800, Thomas Bunce <tbunce@mac.com > wrote:[color=blue]
        > 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
        > --
        > http://mail.python.org/mailman/listinfo/python-list
        >[/color]
        Like any other variable, you need to declare iMatrix before you use it:
        $ python
        Python 2.4 (#1, Dec 28 2004, 12:08:51)
        [GCC 3.3 20030304 (Apple Computer, Inc. build 1495)] on darwin
        Type "help", "copyright" , "credits" or "license" for more information.[color=blue][color=green][color=darkred]
        >>> import random
        >>> import array
        >>> index = 0
        >>> majorlop1 = 4
        >>> iMatrix = array.array('b' )
        >>> listvalues = [1, 2, 3, 4]
        >>> while index < majorlop1:[/color][/color][/color]
        .... index = index + 1
        .... k = random.choice(l istvalues) + 1
        .... iMatrix.append( k)
        ....[color=blue][color=green][color=darkred]
        >>> iMatrix[/color][/color][/color]
        array('b', [3, 5])[color=blue][color=green][color=darkred]
        >>>[/color][/color][/color]

        You should probably look at the wealth of information at
        http://www.python.org/doc - the tutorial is a good start on how to use
        the language, and the library reference has much more depth on the
        array module.
        This module defines an object type which can compactly represent an array of basic values: characters, integers, floating-point numbers. Arrays are mutable sequence types and behave very much like ...

        --
        Sean Blakey
        Saint of Mild Amusement, Evil Genius, Big Geek
        Python/Java/C++/C(Unix/Windows/Palm/Web) developer
        quine = ['print "quine =",quine,"; exec(quine[0])"'] ; exec(quine[0])

        Comment

        • wes weston

          #5
          Re: How do you do arrays

          Thomas Bunce wrote:[color=blue]
          > 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]

          Thomas,
          You can do
          [color=blue][color=green][color=darkred]
          >>> m = [4]
          >>> m[/color][/color][/color]
          [4]

          wes

          Comment

          • Kartic

            #6
            Re: How do you do arrays

            Tom,

            It has to be iMatrix.append( k), not iMatrix[index] = k. Python will
            give an error - list assignment index out of range - for that.
            Just curious - what book are you following?

            -Kartic

            Comment

            • Thomas Bunce

              #7
              Re: How do you do arrays

              Thanks all

              Tom

              Comment

              • beliavsky@aol.com

                #8
                Re: How do you do arrays

                If you want do numerical calculations with vectors and matrices, you
                should probably use the Numarray module. Python's built-in lists are
                not intended for heavy numerical computations. They are more flexible
                than the arrays in languages like C and Fortran in that they can store
                elements of different types. One can write, for example,
                x = ["dog",1,2.3] .

                Comment

                • Thomas Bunce

                  #9
                  Re: How do you do arrays

                  Tryed it and this is what I got (I did go to the web sight)

                  tom(h=500)$ /tmp/501/Cleanup\ At\ Startup/ptesting-128981347.87.py .command; exit
                  Input the maximu number of tvalue: 114
                  Traceback (most recent call last):
                  File "/Users/tom/Desktop/ptesting.py", line 20, in ?
                  iMatrix[index] = k
                  IndexError: list assignment index out of range
                  logout
                  [Process completed]

                  The complete listing:

                  #!/usr/bin/python
                  import random
                  import sys
                  import array
                  #
                  ### generates a list of numbers between 1 and target
                  ### and uses 23 % of these values.
                  #

                  iMatrix = []

                  tvalue = input('Input the maximu number of tvalue: ')
                  majorlop1 = int tvalue * .23)
                  listvalues = range(tvalue)

                  sep = '- '
                  index = 0
                  while index < majorlop1:
                  index = index + 1
                  k = random.choice(l istvalues) + 1
                  iMatrix[index] = k

                  while index < majorlop1:
                  print '- %s %s' % (iMatrix[index], sep)
                  #
                  ###
                  #
                  I would like to set the size of the List/array independent
                  of having to intialialize it prior to use.
                  If it help I will say the bad works I am using OSX

                  Thanks Tom
                  In article <1107284817.848 551.79000@c13g2 000cwb.googlegr oups.com>,
                  "Kartic" <kartic.krishna murthy@gmail.co m> wrote:
                  [color=blue]
                  > Tom,
                  >
                  > Before you use iMatrix[index], you have to tell python to use iMatrix
                  > as an array. You will do that using iMatrix = [] *outside* the loop.
                  >
                  > iMatrix = []
                  > while index < majorlop1: # rest of the loop statements
                  >
                  > Since you are new, please take a look at the Python tutorial to get you
                  > started.
                  > http://docs.python.org/tut/tut.html
                  >
                  > Thanks,
                  > -Kartic[/color]

                  Comment

                  • wes weston

                    #10
                    Re: How do you do arrays

                    Thomas,
                    If you were allowed to do what you're doing, the
                    list first element would be getting skipped as "index"
                    is always > 0. The thing is, you don't want the "index"
                    var at all for adding to the list; just do jMatrix.append( k).
                    You can iterate over the list with
                    for x in jMatrix:
                    print x

                    Is it basic that indexes from 1 vs. 0? 'just about
                    completely forgotten basic.
                    wes

                    Thomas Bunce wrote:[color=blue]
                    > Tryed it and this is what I got (I did go to the web sight)
                    >
                    > tom(h=500)$ /tmp/501/Cleanup\ At\ Startup/ptesting-128981347.87.py .command; exit
                    > Input the maximu number of tvalue: 114
                    > Traceback (most recent call last):
                    > File "/Users/tom/Desktop/ptesting.py", line 20, in ?
                    > iMatrix[index] = k
                    > IndexError: list assignment index out of range
                    > logout
                    > [Process completed]
                    >
                    > The complete listing:
                    >
                    > #!/usr/bin/python
                    > import random
                    > import sys
                    > import array
                    > #
                    > ### generates a list of numbers between 1 and target
                    > ### and uses 23 % of these values.
                    > #
                    >
                    > iMatrix = []
                    >
                    > tvalue = input('Input the maximu number of tvalue: ')
                    > majorlop1 = int tvalue * .23)
                    > listvalues = range(tvalue)
                    >
                    > sep = '- '
                    > index = 0
                    > while index < majorlop1:
                    > index = index + 1
                    > k = random.choice(l istvalues) + 1
                    > iMatrix[index] = k
                    >
                    > while index < majorlop1:
                    > print '- %s %s' % (iMatrix[index], sep)
                    > #
                    > ###
                    > #
                    > I would like to set the size of the List/array independent
                    > of having to intialialize it prior to use.
                    > If it help I will say the bad works I am using OSX
                    >
                    > Thanks Tom
                    > In article <1107284817.848 551.79000@c13g2 000cwb.googlegr oups.com>,
                    > "Kartic" <kartic.krishna murthy@gmail.co m> wrote:
                    >
                    >[color=green]
                    >>Tom,
                    >>
                    >>Before you use iMatrix[index], you have to tell python to use iMatrix
                    >>as an array. You will do that using iMatrix = [] *outside* the loop.
                    >>
                    >>iMatrix = []
                    >>while index < majorlop1: # rest of the loop statements
                    >>
                    >>Since you are new, please take a look at the Python tutorial to get you
                    >>started.
                    >>http://docs.python.org/tut/tut.html
                    >>
                    >>Thanks,
                    >>-Kartic[/color][/color]

                    Comment

                    • beliavsky@aol.com

                      #11
                      Re: How do you do arrays

                      wes weston wrote:[color=blue]
                      > Thomas,
                      > If you were allowed to do what you're doing, the
                      > list first element would be getting skipped as "index"
                      > is always > 0. The thing is, you don't want the "index"
                      > var at all for adding to the list; just do jMatrix.append( k).
                      > You can iterate over the list with
                      > for x in jMatrix:
                      > print x
                      >
                      > Is it basic that indexes from 1 vs. 0? 'just about
                      > completely forgotten basic.
                      > wes[/color]

                      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
                      specified. VB.NET, Microsoft's successor to Visual Basic, requires
                      arrays to start with zero.

                      Comment

                      • Kartic

                        #12
                        Re: How do you do arrays

                        Tom - I answered your question even before you posted it!

                        You have to use iMatrix.append( k) and NOT iMatrix[index] = k.

                        Also, what do you expect out of:
                        while index < majorlop1:
                        print '- %s %s' % ( iMatrix[index], sep)

                        This loop will never get executed because your previous loop finishes
                        due to the same condition index < majorlop1.

                        I am not sure what book you are using but I don't think it is a very
                        good one.
                        [color=blue]
                        > I would like to set the size of the List/array independent
                        > of having to intialialize it prior to use.[/color]

                        You can do the following when you allocate your list.
                        iMatrix = [''] * size # where size is an integer
                        and this will give you a list of size empty elements. If you do this,
                        do not use iMatrix.append( ). Use the array notation like you have in
                        you code currently.

                        Thanks
                        -Kartic

                        Comment

                        • Thomas Bunce

                          #13
                          Re: How do you do arrays

                          Learning Python O'Reilly book and Python In A Nut Shell and about 2 inchs
                          printed of Web information

                          Tom

                          In article <1107286861.714 451.288010@f14g 2000cwb.googleg roups.com>,
                          "Kartic" <kartic.krishna murthy@gmail.co m> wrote:
                          [color=blue]
                          > Tom,
                          >
                          > It has to be iMatrix.append( k), not iMatrix[index] = k. Python will
                          > give an error - list assignment index out of range - for that.
                          > Just curious - what book are you following?
                          >
                          > -Kartic[/color]

                          Comment

                          • Thomas Bunce

                            #14
                            Re: How do you do arrays


                            It was when I saw a use of complex numbers as a usable statement I became
                            interested in Python

                            Tom


                            In article <1107288521.396 952.154000@z14g 2000cwz.googleg roups.com>,
                            beliavsky@aol.c om wrote:
                            [color=blue]
                            > If you want do numerical calculations with vectors and matrices, you
                            > should probably use the Numarray module. Python's built-in lists are
                            > not intended for heavy numerical computations. They are more flexible
                            > than the arrays in languages like C and Fortran in that they can store
                            > elements of different types. One can write, for example,
                            > x = ["dog",1,2.3] .[/color]

                            Comment

                            • Dan Perl

                              #15
                              Re: How do you do arrays

                              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=blue]
                              >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]


                              Comment

                              Working...