By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,271 Members | 1,722 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,271 IT Pros & Developers. It's quick & easy.

Need array help

P: n/a
I am switching from microsoft visual basic programming to python
programming. In microsoft
visual basic you can Dim a variable so that you can add variables by
changing the number
on the end of the variable as in the following example;

Dim acct(100)

numoffiles=4
data=10
ct=1
while ct <numoffiles
acctfile(ct) = data
ct= ct + 1
data= data + ct
Wend
The results are;
acctfile(1)=10
acctfile(2)=12
acctfile(3)=15

And you can compare the values of the new variables;
if acctfile(1) acctfile(2) then print "yes"
if acctfile(2) acctfile(1) then print "yes"

when I try to create acctfile(ct) = data I get the following error;
***can't assign to function call. Then it gives the program line of the
problem
Here is the progam in python;

numoffiles=4
data=10
ct=1

while ct != numoffiles:
acctfile(ct) =data
ct += 1
data= data + ct
print acctfile(ct)

Does anybody know how this is done in Python?
Oct 6 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Marion, I think you should start with the tutorials. To access an array
in Python, use brackets not parens. so, instead of "acctfile(ct) =
data" try "acctfile[ct] = data". The tutorials are excellent and if you
are learning from vb, you can go through them pretty quickly.

On Oct 6, 10:25 am, "Marion Long Jr" <mlon...@verizon.netwrote:
I am switching from microsoft visual basic programming to python
programming. In microsoft
visual basic you can Dim a variable so that you can add variables by
changing the number
on the end of the variable as in the following example;

Dim acct(100)

numoffiles=4
data=10
ct=1
while ct <numoffiles
acctfile(ct) = data
ct= ct + 1
data= data + ct
Wend
The results are;
acctfile(1)=10
acctfile(2)=12
acctfile(3)=15

And you can compare the values of the new variables;
if acctfile(1) acctfile(2) then print "yes"
if acctfile(2) acctfile(1) then print "yes"

when I try to create acctfile(ct) = data I get the following error;
***can't assign to function call. Then it gives the program line of the
problem
Here is the progam in python;

numoffiles=4
data=10
ct=1

while ct != numoffiles:
acctfile(ct) =data
ct += 1
data= data + ct
print acctfile(ct)

Does anybody know how this is done in Python?
Oct 6 '06 #2

P: n/a
Marion Long Jr wrote:
I am switching from microsoft visual basic programming to python
programming. In microsoft
visual basic you can Dim a variable so that you can add variables by
changing the number
on the end of the variable as in the following example;

Dim acct(100)

numoffiles=4
data=10
ct=1
while ct <numoffiles
acctfile(ct) = data
ct= ct + 1
data= data + ct
Wend
The results are;
acctfile(1)=10
acctfile(2)=12
acctfile(3)=15

And you can compare the values of the new variables;
if acctfile(1) acctfile(2) then print "yes"
if acctfile(2) acctfile(1) then print "yes"

when I try to create acctfile(ct) = data I get the following error;
***can't assign to function call. Then it gives the program line of the
problem
Here is the progam in python;

numoffiles=4
data=10
ct=1

while ct != numoffiles:
acctfile(ct) =data
ct += 1
data= data + ct
print acctfile(ct)

Does anybody know how this is done in Python?

You have two choices in Python: list or array
For simple lists of things use a list. If you want to
do vector math or matrix-type calculations look at the
array module.

For you simple example, here is the python code:

acctfile=[]
numoffiles=4
data=10
ct=1
for i in xrange(numoffiles-1):
acctfile.append(data)
ct=ct+1
data=data+ct
There are other ways, but this is closest to what you are
asking for (which I assume is a trivial example).

-Larry Bates
Oct 6 '06 #3

P: n/a
Others posted answer to your question.

If you are serious about programming in Python, I highly recommend that
you don't try to think in terms of "I did this in Visual Basic, how do
I do this in Python". You'll end up with Python code that are nothing
but a VB look alike. As recommended by others, go througth the
tutorial. It will be time well spent.
Marion Long Jr wrote:
I am switching from microsoft visual basic programming to python
programming. In microsoft
visual basic you can Dim a variable so that you can add variables by
changing the number
on the end of the variable as in the following example;

Dim acct(100)

numoffiles=4
data=10
ct=1
while ct <numoffiles
acctfile(ct) = data
ct= ct + 1
data= data + ct
Wend
The results are;
acctfile(1)=10
acctfile(2)=12
acctfile(3)=15

And you can compare the values of the new variables;
if acctfile(1) acctfile(2) then print "yes"
if acctfile(2) acctfile(1) then print "yes"

when I try to create acctfile(ct) = data I get the following error;
***can't assign to function call. Then it gives the program line of the
problem
Here is the progam in python;

numoffiles=4
data=10
ct=1

while ct != numoffiles:
acctfile(ct) =data
ct += 1
data= data + ct
print acctfile(ct)

Does anybody know how this is done in Python?
Oct 6 '06 #4

P: n/a
Larry Bates wrote:
If you want to
do vector math or matrix-type calculations look at the
array module.
The array module in the standard library does not provide such capabilities. If
you need them, look at numpy.

http://numpy.scipy.org

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Oct 6 '06 #5

P: n/a
"Marion Long Jr" <ml*****@verizon.netwrote in message
news:EkwVg.362$P92.105@trndny02...
>I am switching from microsoft visual basic programming to python
programming.
Also, in anticipation of one of the most FA'ed Q's among VB->Python
migrators, you must always include the parens after a function name to
invoke the function. That is, to call the method MethodWithNoArguments, you
cannot write (as one would in VB):

val = MethodWithNoArguments

This simply assigns the method MethodWithNoArguments to name 'val'. To
invoke the method and assign the returned value to val, write:

val = MethodWithNoArguments()

And please don't ask for Python to be changed to be more VB-like in this
regard - let Python be Python! :)

And welcome to the Python world!

-- Paul
Oct 6 '06 #6

P: n/a
Robert Kern wrote:
Larry Bates wrote:
>If you want to
do vector math or matrix-type calculations look at the
array module.

The array module in the standard library does not provide such
capabilities. If you need them, look at numpy.

http://numpy.scipy.org
I stand corrected.

-Larry
Oct 6 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.