0

I'm using the SciPy function kruskal to investigate a statistical database. The SciPy function takes a number of lists as arguments:

from scipy.stats import kruskal

kruskal([1,2,3,4],[5,6,7,8])

(5.3333333333333286, 0.020921335337794052)

kruskal([1,2,3,4],[5,6,7,8],[9,10,11,12])

(9.8461538461538467, 0.007276706499332492)

(I'm just using integers here to show where the datapoints would be - the real datapoints would obviously not be integers)

However if I take a list of lists and try to pass it to the kruskal function this does not work

a=[[1,2,3,4],[5,6,7,8]]
>>> kruskal(a)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\site-packages\scipy\stats\stats.py", line 4188, in kruskal
    raise ValueError("Need at least two groups in stats.kruskal()")
ValueError: Need at least two groups in stats.kruskal()

After some researching I thought I'd identified the problem - that the arguments needed to be a tuple of lists, rather than a list of lists, but this doesn't work either

kruskal(tuple(a))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\site-packages\scipy\stats\stats.py", line 4188, in kruskal
    raise ValueError("Need at least two groups in stats.kruskal()")

ValueError: Need at least two groups in stats.kruskal()

Thanks in advance for any input.

eoghanf
  • 1
  • 1

1 Answers1

1

Python can accept a dynamic sequence to pass in several arguments, as you intent, but you have to declare that explicitly.

The syntax to do that is prefixing your argumetns list with an *:

>>> a=[[1,2,3,4],[5,6,7,8]]
>>> kruskal(*a)

Otherwise, Python, predictable as it is, will just do what you asked: pass your one object (be it a list of lists, tuple of lists, etc... it still a Python object), as a positional parameter to the function called.

It is in brief in the official Python docs, but extensively exemplified in books and tutorials: https://docs.python.org/2/tutorial/controlflow.html#arbitrary-argument-lists

jsbueno
  • 99,910
  • 10
  • 151
  • 209