Need array help

Larry Bates larry.bates at websafe.com
Fri Oct 6 13:59:14 EDT 2006


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



More information about the Python-list mailing list