Array of 2^n
Chris Barker
chrishbarker at home.net
Mon Oct 22 13:48:56 EDT 2001
Moshe wrote:
>
> Okay, python newbie here. I'm liking what I see, but it seems like there
> are holes in the features of python's objects. I remember programming in
> squeak (an extension of smalltalk), and you could do complex operations on
> lists, and it would apply the result to each item. I want to make a n-item
> list such that mylist[i] = 2**i. How can I generate such a list in Python?
IF you really want a list, you have been given a number of options. If,
however, you are doing this a lot, with large sequences of numbers, you
are probably doing other math operations as well, and you want a nice
fast way to do operations on sequences of numbers that has a clean
sequence. For this you want the Numeric module:
http://sourceforge.net/projects/numpy
Then you can do:
from Numeric import *
myArray = 2**arange(i)
You can also do all kinds fo nifty element-wise math with all the basic
operators and math functions. Here is a quick example for the root mean
square values of a rank-1 array:
sqrt(sum(array**2)/(len(myArray))
Numeric has many other nifty features as well. If you are working with
numbers, it is a wonderful tool.
-Chris
--
Christopher Barker,
Ph.D.
ChrisHBarker at home.net --- --- ---
http://members.home.net/barkerlohmann ---@@ -----@@ -----@@
------@@@ ------@@@ ------@@@
Oil Spill Modeling ------ @ ------ @ ------ @
Water Resources Engineering ------- --------- --------
Coastal and Fluvial Hydrodynamics --------------------------------------
------------------------------------------------------------------------
More information about the Python-list
mailing list