相当于R“拆分"功能的Python [英] Python equivalent of R "split"-function

查看:67
本文介绍了相当于R“拆分"功能的Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在R中,您可以根据另一个向量的因子来分割向量:

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

因此,根据另一个列表的值(根据因子的顺序)将一个列表(以python表示)分组.

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

除了itertools.groupby方法之外,在python中是否还有其他方便的方法?

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

推荐答案

在您的示例中,b中的每个元素看起来都包含将节点存储在其中的1索引列表. Python缺少R似乎具有的自动数字变量,因此我们将返回一个列表元组.如果您可以执行零索引列表,并且只需要两个列表(即,对于R用例,则1和2是唯一的值,在python中,它们将分别为0和1)

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])

然后您可以使用itertools.compress:

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

如果您需要更多常规输入(多个数字),则类似以下的内容将返回一个n元组:

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])

这篇关于相当于R“拆分"功能的Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆