生成序列的字符数

时间:2016-07-04 18:32:42

标签: python string

我有一个字符串,如' ....(((...(... ',我必须生成另一个字符串' ss(4)h5( 3)SS(3)H 2(2)SS(3)”。

''对应'ss'和连续''的数量。在括号内。

'('对应'h5'和连续数'('在括号内。

目前我能够获得输出' ss(4)h5(3)ss(3)',我的代码忽略了最后两个字符序列。 这就是我到目前为止所做的事情

df[1:18, 3, drop = FALSE]

如何修改代码以获得所需的输出?

2 个答案:

答案 0 :(得分:4)

我不知道如何修改现有代码,但对我来说,这可以使用itertools.groupby非常简洁和热情地完成。请注意,我不确定您预期输出中的'h2'是否为拼写错误,或者它应该是'h5',我假设。{/ p>

from itertools import chain, groupby

string = '....(((...((...'

def character_count(S, labels): # this allows you to customize the labels you want to use
    for K, G in groupby(S):
        yield labels[K], '(', str(sum(1 for c in G)), ')' # sum() counts the number of items in the iterator G

output = ''.join(chain.from_iterable(character_count(string, {'.': 'ss', '(': 'h5'}))) # joins the components into a single string
print(output)

# >>> ss(4)h5(3)ss(3)h5(2)ss(3)

答案 1 :(得分:1)

@Kelvin的答案很棒,但是如果你想自己定义一个函数,你可以这样做:

def h5ss(x):
    names = {".": "ss", "(": "h5"}
    count = 0
    current = None
    out = ""
    for i in x:
        if i == current:
            count += 1
        else:
            if current is not None:
                out += "{}({})".format(names[current], count)
            count = 1
            current = i
    if current is not None:
        out += "{}({})".format(names[current], count)
    return out