syntax

Chris Rebert clp2 at rebertia.com
Tue Jan 19 02:23:34 CET 2010


On Mon, Jan 18, 2010 at 4:53 PM, marlowe <marlowequart at hotmail.com> wrote:
> I wrote this program, but i have a feeling like there might be a more
> practical way of writing it. Can someone give me an idea of how to
> simplify this? Here is an example of the csv file i am using. This
> program calculates the exponential moving average of the 20 day range.
<snip>

> H=float(data[40][2])+float(data[41][2])+float(data[42][2])+float(data
> [43][2])\
> +float(data[44][2])+float(data[45][2])+float(data[46][2])+float(data
> [47][2])\
> +float(data[48][2])+float(data[49][2])+float(data[50][2])+float(data
> [51][2])\
> +float(data[52][2])+float(data[53][2])+float(data[54][2])+float(data
> [55][2])\
> +float(data[56][2])+float(data[57][2])+float(data[58][2])+float(data
> [59][2])\
> +float(data[60][2])

H = sum(float(data[i][2]) for i in xrange(40,61))

> L=float(data[40][3])+float(data[41][3])+float(data[42][3])+float(data
> [43][3])\
> +float(data[44][3])+float(data[45][3])+float(data[46][3])+float(data
> [47][3])\
> +float(data[48][3])+float(data[49][3])+float(data[50][3])+float(data
> [51][3])\
> +float(data[52][3])+float(data[53][3])+float(data[54][3])+float(data
> [55][3])\
> +float(data[56][3])+float(data[57][3])+float(data[58][3])+float(data
> [59][3])\
> +float(data[60][3])

H = sum(float(data[i][3]) for i in xrange(40,61))

Might I suggest you learn more about comprehensions and looping?:
http://docs.python.org/tutorial/datastructures.html#list-comprehensions
http://www.python.org/dev/peps/pep-0289/
http://docs.python.org/library/functions.html#range
http://docs.python.org/library/functions.html#sum

Cheers,
Chris
--
http://blog.rebertia.com



More information about the Python-list mailing list