“分裂” RLE (groupby) 的输出取决于定义的值(用于分割 RLE 的“字符”)

发布于 2024-10-05 05:53:41 字数 953 浏览 0 评论 0原文

考虑“字符串”(将其视为数字数组)

0 0 1 8 8 8 1 0

RLE(“groupby”)是:

[(0,2), (1, 1), (8,3), (1, 1), (0, 1)]

然后,我们用前面元素的游程长度之和来丰富上述 RLE。

因此,上述内容的丰富版本变为:

[(0, (0,2)), (0+2, (1, 1)), (0+2+1, (8,3)), (0+1+2+3, (1, 1)), (0+1+2+3+1, (0, 1))]

The "string" split on 1:

0 0 , 8 8 8 , 0

RLE split on 1

[(0,2)] , [(8,3)] , [(0, 1)]

The "string" split on 8:

0 0 1 , , , 1 0

RLE split on 8

[(0,2), (1, 1)] , , , [(1, 1), (0, 1)]

注意:在我的示例中,我引用了“RLE split on Z”列出而不丰富它们。事实并非如此。我把它们留下来以减少混乱。例如,“RLE split on 1”实际上应该被视为:

[(0, (0,2))] , [(0+2+1, (8,3))] , [(0+1+2+3+1, (0, 1)]

如何在 Z 上实现此“RLE split”(= 1, 8;在本例中)

可以留空数组(分割后)。

也许是一个聪明的列表比较? (使用嵌套有附加的 for 循环似乎更容易解决)

Consider the "string" ( treat it as an array of digits )

0 0 1 8 8 8 1 0

The RLE ( "groupby" ) is:

[(0,2), (1, 1), (8,3), (1, 1), (0, 1)]

We then enrich the above RLE with the sum of the run lengths of the previous elements.

Hence, the enriched version of the above becomes:

[(0, (0,2)), (0+2, (1, 1)), (0+2+1, (8,3)), (0+1+2+3, (1, 1)), (0+1+2+3+1, (0, 1))]

The "string" split on 1:

0 0 , 8 8 8 , 0

RLE split on 1

[(0,2)] , [(8,3)] , [(0, 1)]

The "string" split on 8:

0 0 1 , , , 1 0

RLE split on 8

[(0,2), (1, 1)] , , , [(1, 1), (0, 1)]

Note : In my examples, I have cited the "RLE split on Z" lists without enriching them. This would not be so. I left them out to reduce clutter. For example, the "RLE split on 1" should really be treated as:

[(0, (0,2))] , [(0+2+1, (8,3))] , [(0+1+2+3+1, (0, 1)]

How can I achieve this "RLE split" on Z ( = 1, 8; in this case )

It's fine to leave out empty arrays ( after split ).

Perhaps a clever list comp.? ( it seems a little easier to solve with a for loop with an append nested within )

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

漫雪独思 2024-10-12 05:53:41

只是为了展示如何使用,我强烈建议您不要使用这种

“优雅”的丑陋方式:

>>> data
[0, 0, 1, 8, 8, 8, 4, 4, 1, 0]
>>> def fromDataToSplitRLE(dat,n):
    RLE=[(k,len(tuple(g))) for k,g in itertools.groupby(dat)]
    tmp=tuple(zip(*RLE))
    return [list(g) for k,g in itertools.groupby((zip((sum(tmp[1][:i]) for i in range(len(tmp[1]))) ,(zip(*tmp)))),lambda x:x[1][0]!=n) if k]

>>> fromDataToSplitRLE(data,1)
[[(0, (0, 2))], [(3, (8, 3)), (6, (4, 2))], [(9, (0, 1))]]

Just for show the way how, I strongly advise you not to use this

"elegant" ugly way:

>>> data
[0, 0, 1, 8, 8, 8, 4, 4, 1, 0]
>>> def fromDataToSplitRLE(dat,n):
    RLE=[(k,len(tuple(g))) for k,g in itertools.groupby(dat)]
    tmp=tuple(zip(*RLE))
    return [list(g) for k,g in itertools.groupby((zip((sum(tmp[1][:i]) for i in range(len(tmp[1]))) ,(zip(*tmp)))),lambda x:x[1][0]!=n) if k]

>>> fromDataToSplitRLE(data,1)
[[(0, (0, 2))], [(3, (8, 3)), (6, (4, 2))], [(9, (0, 1))]]
夏雨凉 2024-10-12 05:53:41
import itertools

def get_rle(list_of_digits, split_on=None):
    count = 0
    rle = []
    active_group = []
    rle_app = rle.append
    for item, group in itertools.groupby(list_of_digits):
        L = len(list(group))
        if item == split_on:
            rle_app(active_group)
            active_group = []
        else:
            active_group.append((count, (item, L)))
        count += L

    rle_app(active_group)
    return rle

list_of_digits = map(int, '0 0 1 8 8 8 1 0'.split())
print get_rle(list_of_digits)
print get_rle(list_of_digits, 8)
print get_rle(list_of_digits, 1)

aaron@aaron-laptop:~/code/tmp$ python rle.py
[[(0, (0, 2)), (2, (1, 1)), (3, (8, 3)), (6, (1, 1)), (7, (0, 1))]]
[[(0, (0, 2)), (2, (1, 1))], [(6, (1, 1)), (7, (0, 1))]]
[[(0, (0, 2))], [(3, (8, 3))], [(7, (0, 1))]]
import itertools

def get_rle(list_of_digits, split_on=None):
    count = 0
    rle = []
    active_group = []
    rle_app = rle.append
    for item, group in itertools.groupby(list_of_digits):
        L = len(list(group))
        if item == split_on:
            rle_app(active_group)
            active_group = []
        else:
            active_group.append((count, (item, L)))
        count += L

    rle_app(active_group)
    return rle

list_of_digits = map(int, '0 0 1 8 8 8 1 0'.split())
print get_rle(list_of_digits)
print get_rle(list_of_digits, 8)
print get_rle(list_of_digits, 1)

aaron@aaron-laptop:~/code/tmp$ python rle.py
[[(0, (0, 2)), (2, (1, 1)), (3, (8, 3)), (6, (1, 1)), (7, (0, 1))]]
[[(0, (0, 2)), (2, (1, 1))], [(6, (1, 1)), (7, (0, 1))]]
[[(0, (0, 2))], [(3, (8, 3))], [(7, (0, 1))]]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文