确定字符串中字符重复的长度和位置(Determining length and position of character repeats in string)

编程入门 行业动态 更新时间:2024-10-27 20:30:32
确定字符串中字符重复的长度和位置(Determining length and position of character repeats in string)

假定一个字符串s可能包含几个相邻的短划线。 为了简单起见,我们称这些事件为“重复动机”。 例如,以下字符串s包含五个重复动机的短划线,即长度为3,2,6,5和1。

s = "abcde---fghij--klmnopq------rstuvw-----xy-z"

我试图想出Python代码,它返回每个重复动机字符串中的相应长度和相应位置。 优先,代码返回一个元组列表,每个元组都是格式(长度,位置)。

sought_function(s) # [(3,5), (2,13), (6,22), (5,34), (1,41)]

你有什么建议如何启动这个代码?

Assume a string s that may contain several adjacent occurrences of dashes. For the sake of simplicity, let's call each of these occurrences a "repeat motive". For example, the following string s contains five repeat motives of dashes, namely of length 3,2,6,5 and 1.

s = "abcde---fghij--klmnopq------rstuvw-----xy-z"

I am trying to come up with Python code that returns the respective length and the respective position within the string of each of the repeat motives. Preferentially, the code returns a list of tuples, with each tuple being of format (length, position).

sought_function(s) # [(3,5), (2,13), (6,22), (5,34), (1,41)]

Would you have any suggestions as to how to start this code?

最满意答案

你可以使用groupby :

s = "abcde---fghij--klmnopq------rstuvw-----xy-z" from itertools import groupby [(next(g)[0], sum(1 for _ in g) + 1) for k, g in groupby(enumerate(s), lambda x: x[1]) if k == "-"] # [(5, 3), (13, 2), (22, 6), (34, 5), (41, 1)]

或者@Willem评论说,用len替换sum :

[(next(g)[0], len(list(g)) + 1) for k, g in groupby(enumerate(s), lambda x: x[1]) if k == "-"] # [(5, 3), (13, 2), (22, 6), (34, 5), (41, 1)]

You can use groupby:

s = "abcde---fghij--klmnopq------rstuvw-----xy-z" from itertools import groupby [(next(g)[0], sum(1 for _ in g) + 1) for k, g in groupby(enumerate(s), lambda x: x[1]) if k == "-"] # [(5, 3), (13, 2), (22, 6), (34, 5), (41, 1)]

Or as @Willem commented, replace the sum with len:

[(next(g)[0], len(list(g)) + 1) for k, g in groupby(enumerate(s), lambda x: x[1]) if k == "-"] # [(5, 3), (13, 2), (22, 6), (34, 5), (41, 1)]

更多推荐

本文发布于:2023-08-05 08:05:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1429721.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   长度   字符   位置   Determining

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!