How do I let a function accept a variable number of lists:
Author Message
How do I let a function accept a variable number of lists:

I'm trying out the (very nice) stats module from
http://www.*-*-*.com/
I'm using Win NT4 and  Python 2.0:

----------------------------------------
import pstat, stats

def DoAnova():
''' A command line interface to calculate one-way ANOVA. '''
numPoints = input('How many data points in each group? ')
numGroups = input('How many groups are there (2 minimum)? ')
li = [0]
for i in range(numGroups - 1):
li.append(0)
for i in range(numGroups):
li[i] = [0]*numPoints
for group in range(numGroups):
for point in range(numPoints):
li[group][point] = input('Enter a data point: ')
print li

x = stats.F_oneway(li[0], li[1])
print x

DoAnova()
---------------------------------------

Problem:
The above works properly for 2 lists ONLY. e.g.: [1,2], [3,4]
It returns:
[[1, 2], [3, 4]]
(8.0, 0.10557280898846583)

The problem is this line:
x = stats.F_oneway(li[0], li[1])

I've just set it to accept the first two lists.
The docstring for lF_oneway includes:
"Usage:   F_oneway(*lists)   where *lists is any number of lists, one
per treatment group"

Question:
How do I automatically pass the correct number of lists to
stats.F_oneway()?

If I could turn the list of lists into a series of lists i.e:
li = [[1, 2], [3, 4], [5,6]] --> li = [1,2], [3,4], [5,6]]
then I could presumably just pass li.

Sun, 16 Nov 2003 08:40:22 GMT
How do I let a function accept a variable number of lists:

...

Quote:
> The problem is this line:
> x = stats.F_oneway(li[0], li[1])

> I've just set it to accept the first two lists.
> The docstring for lF_oneway includes:
> "Usage:   F_oneway(*lists)   where *lists is any number of lists, one
> per treatment group"

> Question:
> How do I automatically pass the correct number of lists to
> stats.F_oneway()?

That's exactly what the star-syntax does in the call, in
python 2.

Quote:
> If I could turn the list of lists into a series of lists i.e:
> li = [[1, 2], [3, 4], [5,6]] --> li = [1,2], [3,4], [5,6]]
> then I could presumably just pass li.

Right.  You call:
x = stats.F_oneway(*li)
(note the asterisk) and that is just about what happens.

Alex

Sun, 16 Nov 2003 14:36:49 GMT

 Page 1 of 1 [ 2 post ]

Relevant Pages