[Tutor] raw_input and converting string to digit
Mic Forster
micforster@yahoo.com
Thu Feb 27 23:37:01 2003
Hi Guys,
The purpose of this script is to obtain an expected
species-abundance curve given an observed curve. The
script works fine for this purpose but it is
cumbersome to implement. What I am trying to do is to
open this file, enter in a number that represents the
fundamental biodiversity number (theta) and another
that represents the community size (jM), let the
program do its stuff and then give the expected
abundance output.
I am unsure of the correct syntax to convert a string
from raw_input into a digit. Here is what I have (any
additional comments on any aspect of this code is more
than welcome):
>>>from random import random as rnd
>>>theta = []
>>>def addTheta():
theta.append(raw_input(‘Fundamental biodiversity
number is: ‘))
>>>jM = []
>>>def addJM:
jM.append(raw_input(‘The metacommmunity size is:
‘))
>>>class growList(list):
'''A list that does as in Perl: Grows when
we assign to new indices. I.e. x[1] = 5 will
work if x was previously empty.'''
def __setitem__(self, i, val):
while i >= len(self):
self.append(None)
list.__setitem__(self, i, val)
def __getitem__(self, i):
'Return 0 for unused slots'
if i < len(self):
return list.__getitem__(self, i)
else:
return 0
>>>abund = growList()
>>>abund[1] = 1
>>>cumul = growList()
>>>def nsp():
'''Number of species must be the same as the size
of the abundence list (I guess). Note that first
position is unused to match the 1-based algorithm
syntax.'''
return len(abund) - 1
>>>for j in range(2, jM + 1):
x = rnd()
if x < (theta / float(theta + j - 1)):
abund.append(1)
else:
cumul[1] = abund[1] / float(j-1)
for i in range(2, nsp()+1):
cumul[i] = cumul[i-1] + abund[i] /
float(j-1)
i = 1
while x >= cumul[i]:
i += 1
abund[i] += 1
>>>for abundance in zip(abund):
print '%s' % abundance
__________________________________________________
Do you Yahoo!?
Yahoo! Tax Center - forms, calculators, tips, more
http://taxes.yahoo.com/