假定一个字符串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)]更多推荐
发布评论