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

1 で分割された「文字列」:

0 0 , 8 8 8 , 0

1 の RLE 分割

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

8 で分割された「文字列」:

0 0 1 , , , 1 0

8 で RLE 分割

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

注 : 私の例では、「Z での RLE 分割」リストを強調せずに引用しました。これはそうではないでしょう。混乱を減らすためにそれらを省略しました。たとえば、「RLE split on 1」は、実際には次のように処理する必要があります。

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

Z (= 1、8;この場合)でこの「RLE分割」を達成するにはどうすればよいですか

空の配列を省略しても問題ありません ( split の後)。

おそらく賢いリスト構成?( 内にネストされた追加の for ループで解決する方が少し簡単に思えます)

4

2 に答える 2

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))]]
于 2010-11-29T22:27:30.873 に答える
1

方法を示すためだけに、これを使用しないことを強くお勧めします

「エレガントな」醜い方法:

>>> 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))]]
于 2010-11-29T22:36:51.230 に答える