How do you do arrays

T

Thomas Bunce

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(listvalues) + 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
 
K

Kartic

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
 
S

Sean Blakey

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(listvalues) + 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
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.
import random
import array
index = 0
majorlop1 = 4
iMatrix = array.array('b')
listvalues = [1, 2, 3, 4]
while index < majorlop1:
.... index = index + 1
.... k = random.choice(listvalues) + 1
.... iMatrix.append(k)
....

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.
http://docs.python.org/lib/module-array.html
 
W

wes weston

Thomas said:
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(listvalues) + 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

Thomas,
You can do
[4]

wes
 
K

Kartic

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
 
B

beliavsky

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] .
 
T

Thomas Bunce

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(listvalues) + 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
 
W

wes weston

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 said:
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(listvalues) + 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
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
 
B

beliavsky

wes said:
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

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.
 
K

Kartic

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.
I would like to set the size of the List/array independent
of having to intialialize it prior to use.

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
 
T

Thomas Bunce

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

Tom
 
T

Thomas Bunce

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

Tom
 
D

Dan Perl

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(listvalues) + 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
 
T

Thomas Bunce

Thanks
Tom
"Dan Perl" said:
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(listvalues) + 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 said:
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(listvalues) + 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
 
D

Dennis Lee Bieber

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

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.

--
 
E

Erik Max Francis

Dennis said:
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.

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

Alex Martelli

Kartic said:
I am not sure what book you are using but I don't think it is a very
good one.

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,219
Messages
2,571,117
Members
47,729
Latest member
taulaju99

Latest Threads

Top