pythonarrayspandasnumpykruskal-wallis

How to delist of array from np.split() for function argument


Suppose I have data as in array

X =  [ x1 x2 ... xn ]

when I use np.split(X,n) will separate in to this

ARR = [ [arr1] ,[arr2] ,.... [arrn] ]

Now I would get those group of array list into function as an input

In this case scipy.stats.kruskal

https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.mstats.kruskalwallis.html

Here are sample

random_array = np.arange(1000)
list_array = np.array_split(random_array, 4)

as for the example I can use kruskal function to calculate

from scipy.stats import kruskal

kruskal(list_array[0],list_array[1],list_array[2],list_array[3])

The problem is I don't want write iterate list_array[0] to list_array[3] but I want to pass variable list_array into argument direct

kruskal(list_array)

as all data as inside data argument. is there a way to delist array and pass it all array inside as and argument?


Solution

  • This is exactly what the star operator (*) does:

    Instead of kruskal(list_array[0], list_array[1], list_array[2], list_array[3])

    you can write: kruskal(*list_array) for arbitrary lengths of list_array.

    For more information: What does the star and doublestar operator mean in a function call?