Python equivalent of R "split"-function

dorvak

In R, you could split a vector according to the factors of another vector:

> a <- 1:10
  [1]  1  2  3  4  5  6  7  8  9 10
> b <- rep(1:2,5)
  [1] 1 2 1 2 1 2 1 2 1 2

> split(a,b)

   $`1`
   [1] 1 3 5 7 9
   $`2`
   [1]  2  4  6  8 10

Thus, grouping a list (in terms of python) according to the values of another list (according to the order of the factors).

Is there anything handy in python like that, except from the itertools.groupby approach?

John Spong

From your example, it looks like each element in b contains the 1-indexed list in which the node will be stored. Python lacks the automatic numeric variables that R seems to have, so we'll return a tuple of lists. If you can do zero-indexed lists, and you only need two lists (i.e., for your R use case, 1 and 2 are the only values, in python they'll be 0 and 1)

>>> a = range(1, 11)
>>> b = [0,1] * 5

>>> split(a, b)
([1, 3, 5, 7, 9], [2, 4, 6, 8, 10])

Then you can use itertools.compress:

def split(x, f):
    return list(itertools.compress(x, f)), list(itertools.compress(x, (not i for i in f)))

If you need more general input (multiple numbers), something like the following will return an n-tuple:

def split(x, f):
    count = max(f) + 1
    return tuple( list(itertools.compress(x, (el == i for el in f))) for i in xrange(count) )  

>>> split([1,2,3,4,5,6,7,8,9,10], [0,1,1,0,2,3,4,0,1,2])
([1, 4, 8], [2, 3, 9], [5, 10], [6], [7])

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

R equivalent to the Python function "dir"?

From Dev

Is there a Python equivalent to R's sample() function?

From Dev

Python equivalent of R's head and tail function

From Dev

Python numpy or pandas equivalent of the R function sweep()

From Dev

Python's equivalent for R's dput() function

From Dev

Python equivalent of R apply with anonymous function

From Dev

Equivalent of R function 'ave' in Python Pandas

From Dev

Equivalent of split_function() in HIVE

From Dev

Equivalent of split_function() in HIVE

From Dev

Split based on factors alternative of R function in python

From Dev

Equivalent R function for Matlab

From Dev

Equivalent R function for Matlab

From Dev

Browser() equivalent function in Python

From Dev

Is there a Python equivalent to the mahalanobis() function in R? If not, how can I implement it?

From Java

python equivalent of R table

From Dev

Python equivalent of the R operator "%in%"

From Dev

Equivalent of source() of R in Python

From Dev

Equivalent of "table" of R in python

From Dev

Split and Diff function in R

From Dev

R equivalent of the Matlab spy function

From Dev

Mod() function in R equivalent for MatLab?

From Dev

Confusion with split function in Python

From Dev

Python .split() function

From Dev

Python complicated split function

From Dev

The equivalent function of Matlab imfilter in Python

From Dev

Matlab importdata() function equivalent in Python

From Dev

Is there a Swift equivalent to the 'Filter' function in Python?

From Dev

Equivalent matlab damp function for Python

From Dev

Equivalent ruby load function in python