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 19 2396
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
and it is called a "List" in Python parlance.
On Tue, 01 Feb 2005 10:52:45 -0800, Thomas Bunce <tb****@mac.com> wrote: 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 -- http://mail.python.org/mailman/listinfo/python-list
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)
.... iMatrix
array('b', [3, 5])
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
--
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])
Thomas Bunce wrote: 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 m = [4] m
[4]
wes
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
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] .
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
In article <11*********************@c13g2000cwb.googlegroups. com>,
"Kartic" <ka******************@gmail.com> wrote: 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
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: 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 In article <11*********************@c13g2000cwb.googlegroups. com>, "Kartic" <ka******************@gmail.com> wrote:
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
wes weston wrote: 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.
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
Learning Python O'Reilly book and Python In A Nut Shell and about 2 inchs
printed of Web information
Tom
In article <11**********************@f14g2000cwb.googlegroups .com>,
"Kartic" <ka******************@gmail.com> wrote: 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
It was when I saw a use of complex numbers as a usable statement I became
interested in Python
Tom
In article <11**********************@z14g2000cwz.googlegroups .com>, be*******@aol.com wrote: 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] .
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" <tb****@mac.com> wrote in message
news:tb*********************@192.168.1.12... 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
Thanks
Tom
In article <-r********************@rogers.com>, "Dan Perl"
<da*****@mail-me.com> wrote: 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" <tb****@mac.com> wrote in message news:tb*********************@192.168.1.12...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
On 1 Feb 2005 13:03:12 -0800, be*******@aol.com declaimed the following
in comp.lang.python: 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.
-- ================================================== ============ < wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG < wu******@dm.net | Bestiaria Support Staff < ================================================== ============ < Home Page: <http://www.dm.net/~wulfraed/> < Overflow Page: <http://wlfraed.home.netcom.com/> <
Dennis Lee Bieber wrote: 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.
--
Erik Max Francis && ma*@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
Kartic <ka******************@gmail.com> wrote:
... 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
On Tue, 01 Feb 2005 23:12:57 -0800, Erik Max Francis <ma*@alcyone.com>
declaimed the following in comp.lang.python: Basic also has the OPTION BASE instruction, which affects that.
I did say "classic"... Option Base came in with, what, GW-BASIC?
-- ================================================== ============ < wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG < wu******@dm.net | Bestiaria Support Staff < ================================================== ============ < Home Page: <http://www.dm.net/~wulfraed/> < Overflow Page: <http://wlfraed.home.netcom.com/> < This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Canonical Latin |
last post by:
"Leor Zolman" <leor@bdsoft.com> wrote
> "Canonical Latin" <javaplus@hotmail.com> wrote:
>
> > ...
> >But I'm still curious as to the rational of having type
>...
|
by: Matteo Settenvini |
last post by:
Ok, I'm quite a newbie, so this question may appear silly. I'm using
g++ 3.3.x.
I had been taught that an array isn't a lot different from a pointer
(in fact you can use the pointer arithmetics to...
|
by: JezB |
last post by:
What's the easiest way to concatenate arrays ? For example, I want a list of
files that match one of 3 search patterns, so I need something like
DirectoryInfo ld = new DirectoryInfo(searchDir);...
|
by: Michel Rouzic |
last post by:
It's the first time I try using structs, and I'm getting confused with
it and can't make it work properly
I firstly define the structure by this : typedef struct { char *l1; int
*l2; int Nval; }...
|
by: Rob Griffiths |
last post by:
Can anyone explain to me the difference between an element type and a
component type?
In the java literature, arrays are said to have component types, whereas
collections from the Collections...
|
by: Rene Nyffenegger |
last post by:
Hello everyone.
I am not fluent in JavaScript, so I might overlook the obvious.
But in all other programming languages that I know and that
have associative arrays, or hashes, the elements in...
|
by: Robert Bravery |
last post by:
Hi all,
Can some one show me how to achieve a cross product of arrays. So that if I
had two arrays (could be any number) with three elements in each (once again
could be any number) I would get:...
|
by: Doug_J_W |
last post by:
I have a Visual Basic (2005) project that contains around twenty
embedded text files as resources. The text files contain two columns
of real numbers that are separated by tab deliminator, and are...
|
by: mike3 |
last post by:
(I'm xposting this to both comp.lang.c++ and comp.os.ms-
windows.programmer.win32
since there's Windows material in here as well as questions related to
standard
C++. Not sure how that'd go over...
|
by: weaknessforcats |
last post by:
Arrays Revealed
Introduction
Arrays are the built-in containers of C and C++. This article assumes the reader has some experiece with
arrays and array syntax but is not clear on a )exactly how...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |