tree functions daily exercise: Table
Xah Lee
xah at xahlee.org
Tue Jun 21 17:37:53 EDT 2005
oops, another error. The example should be:
Table(f,[1,2,1],[2,6,2]) returns
[[f(1,2),f(1,4),f(1,6)],[f(2,2),f(2,4),f(2,6)]]
> Wouldn't it be more sensible just to take the iterators directly as
> arguments, so for this example you would do:
>
> Table(f, range(1,3), range(2,7,2))
well yes... but this was emulation of Mathematica functions.
(Disclaimer: Mathematica is a trademark of Wolfram Research Inc, who is
not affliated with this project)
What you suggested is a function called Outer in Mathematica. The Table
function is in a sense multi-dimentional version of Range, so they
share syntax form.
Xah
xah at xahlee.org
∑ http://xahlee.org/
Duncan Booth wrote:
> Xah Lee wrote:
>
> > '''Table(f,[iStart,iEnd,iStep]) returns a list of f applied to the
> > range range(iStart,iEnd,iStep). Example: Table(f,[3,10,2]) returns
> > [f(3),f(5),f(7),f(9)] Table(f,[iStart,iEnd,iStep],
> > [jStart,jEnd,jStep], ...) returns a nested list of f(i,j,...) applied
> > thru the iterators. Example: Table(f,[1,3,1],[2,6,2]) returns
> > [[f(1,2),f(1,4),f(1,6)],[f(2,2),f(2,4),f(2,6)]]'''
>
> How does it know when to skip the end value (i.e. act as for the rest of
> Python) and when to include it? Or did you mean:
>
> Table(f,[1,3,1],[2,7,2]) returns
> [[f(1,2),f(1,4),f(1,6)],[f(2,2),f(2,4),f(2,6)]]
>
> Wouldn't it be more sensible just to take the iterators directly as
> arguments, so for this example you would do:
>
> Table(f, range(1,3), range(2,7,2))
>
> That way you have more flexibility (e.g. to do out-of-order ranges such as
> [1,3,2,0,4]), and the code for Table is much simpler since it just has to
> manipulate the lists it was given.
More information about the Python-list
mailing list