连续的字符序列 [英] Consecutive Character Sequences

查看:80
本文介绍了连续的字符序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法[有效]迭代一系列字符来找到N [或更多]连续的等价字符?


所以,例如,字符串taaypiqee88adbbba ;如果在函数调用的参数

中提供的(连续字符数)为2或3,则返回1,因为a,e,8和b表示a,e,8和b。重复2到3次。


感谢您的帮助。

W. Brunswick。

Is there any way to [efficiently] iterate through a sequence of characters to find N [or more] consecutive equivalent characters?

So, for example, the string "taaypiqee88adbbba" would return 1 if the number (of consequtive characters) supplied in the parameters
of the function call was 2 or 3, because "a", "e", 8, and "b" is repeated 2 or 3 times.

Thanks for any assistance.
W. Brunswick.

推荐答案

Walter Brunswick写道:
Walter Brunswick wrote:
有没有办法[有效]迭代一系列字符来找到N [或更多]连续的等价字符?
中提供的(连续字符数)是2或3,则返回1,因为a,e,8和b表示a,e,8和b。重复2到3次。
Is there any way to [efficiently] iterate through a sequence of characters to find N [or more] consecutive equivalent characters?

So, for example, the string "taaypiqee88adbbba" would return 1 if the number (of consequtive characters) supplied in the parameters
of the function call was 2 or 3, because "a", "e", 8, and "b" is repeated 2 or 3 times.




为什么会返回1?这是代替True,还是代表一个

项目数,或者列表中某些东西的位置,或者是什么?


-Peter



Why would it return 1? Is that instead of True, or does it represent a
count of items, or the position of something in the list, or what?

-Peter





Walter Brunswick写道:


Walter Brunswick wrote:
有没有办法[有效]迭代序列找到N [或更多]连续等效字符的字符?

因此,例如,字符串taaypiqee88adbbba如果在函数调用的参数
中提供的(连续字符数)是2或3,则返回1,因为a,e,8和b表示a,e,8和b。重复2到3次。

感谢您的帮助。
W. Brunswick。
Is there any way to [efficiently] iterate through a sequence of characters to find N [or more] consecutive equivalent characters?

So, for example, the string "taaypiqee88adbbba" would return 1 if the number (of consequtive characters) supplied in the parameters
of the function call was 2 or 3, because "a", "e", 8, and "b" is repeated 2 or 3 times.

Thanks for any assistance.
W. Brunswick.




def rep(n):

#当前重复次数

the_rep = 1

#检查前一个字符

last_c =''''

#重复历史

max_rep = {}

for c in s:

#找到重复的字符?

如果c == last_c:

#计算连续多少重复

the_rep + = 1

#repethition(如果有的话)结束,保存以前的代表数

否则:

#之前发生了这个计数吗?

如果max_rep.has_key(the_rep):

#如果是这样,跟踪它有多少次

max_rep [the_rep] + = 1

#否则,将此重复计数添加到历史记录中

else:

max_rep [the_rep] = 1

#reset rep count to looking next block

the_rep = 1

#保存当前字符以与下一个字符进行比较

last_c = c

#检查字符串中的最后一个字符是不是块的一部分

如果max_rep.has_key(the_rep):

max_rep [the_rep] + = 1

else:

max_rep [the_rep] = 1

#finally,我们要求的块大小是否曾经发生过?

if max_rep.has_key(n):

返回1

否则:

返回0


s = ''taaypiqee88adbbba''


for i in range(9):

print rep(i),

"" ;"


0 1 1 1 0 0 0 0 0


"""



def rep(n):
# current repetition count
the_rep = 1
# previous character inspected
last_c = ''''
# history of repetions
max_rep = {}
for c in s:
# duplicate character found?
if c==last_c:
# count how many consecutive dups
the_rep += 1
# repetition (if any) ended, save previous rep count
else:
# has this count occured before?
if max_rep.has_key(the_rep):
# if so, track how many times it has
max_rep[the_rep] += 1
# otherwise, add this rep count to history
else:
max_rep[the_rep] = 1
# reset rep count to look for next block
the_rep = 1
# save current character to compare to next character
last_c = c
# check that last character in string wasn''t part of a block
if max_rep.has_key(the_rep):
max_rep[the_rep] += 1
else:
max_rep[the_rep] = 1
# finally, did the block size we asked for ever occur?
if max_rep.has_key(n):
return 1
else:
return 0

s = ''taaypiqee88adbbba''

for i in range(9):
print rep(i),
"""

0 1 1 1 0 0 0 0 0

"""


" Walter Brunswick" < WA ************* @ sympatico.ca>写道:
"Walter Brunswick" <wa*************@sympatico.ca> wrote:
有没有办法[有效]迭代一系列字符来找到N [或更多]
连续等效字符?
所以,对于例如,字符串taaypiqee88adbbba如果在函数调用的参数中提供的数字(连续的
个字符)是2或3,则返回1,因为a,e,8和b表示a,e,8和b。重复2到3次。

感谢您的帮助。
W. Brunswick。
Is there any way to [efficiently] iterate through a sequence of characters to find N [or more] consecutive equivalent characters?
So, for example, the string "taaypiqee88adbbba" would return 1 if the number (of consequtive characters) supplied in the parameters of the function call was 2 or 3, because "a", "e", 8, and "b" is repeated 2 or 3 times.

Thanks for any assistance.
W. Brunswick.




如果你在2.4,使用itertools.groupby:


导入itertools吧


def hasConsequent(aString,minConsequent):

为_,group in it.groupby(aString):

if len(list(group))> = minConsequent:

return true

返回False

George



If you''re in 2.4, use itertools.groupby:

import itertools as it

def hasConsequent(aString, minConsequent):
for _,group in it.groupby(aString):
if len(list(group)) >= minConsequent:
return True
return False
George


这篇关于连续的字符序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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