1

我有这段代码,每次找到 B 时我都会在其中拆分一个字符串:

string = "AAABAABAAABIAABIAABAA"
s=[x.strip() for x in string.split('B')]
print '\n'.join(s)

输出将是这样的:

'AAA' 'AA' 'AAA' 'IAA' 'IAA' 'AA'

我的问题是,如何在输出之前放置一个范围(char 的第一个位置,char 的最后一个位置),如下所示(0,3)AAA (3,5)AA (5,8)AAA (8,11)IAA

4

2 回答 2

4
>>> import re
>>> groups = (((x.start(), x.end()), x.group()) for x in matches)
>>> matches = re.finditer("[^B]+", "AAABAABAAABIAABIAABAA")
>>> groups = (((x.start(), x.end()), x.group()) for x in matches)
>>> ["{}{}".format(*arg) for arg in groups]
['(0, 3)AAA', '(4, 6)AA', '(7, 10)AAA', '(11, 14)IAA', '(15, 18)IAA', '(19, 21)AA']

编辑:

正如 segfalt 指出的那样,我没有正确阅读这个问题......

>>> s = 0
>>> res = []
>>> for x in "AAABAABAAABIAABIAABAA".split('B'):
...     res.append("{}{}".format((s, s+len(x)), x))
...     s += len(x)
... 
>>> print "".join(res)
(0, 3)AAA(3, 5)AA(5, 8)AAA(8, 11)IAA(11, 14)IAA(14, 16)AA

或者

>>> print "\n".join(res)
(0, 3)AAA
(3, 5)AA
(5, 8)AAA
(8, 11)IAA
(11, 14)IAA
(14, 16)AA
于 2013-05-27T23:55:23.083 回答
1

使用没有导入的简单for循环:

data = "AAABAABAAABIAABIAABAA"

result, count = [], 0
for s in data.split('B'):
    result.append([(count, count+len(s)), s])
    count += len(s)
print(result)

输出:

[[(0, 3), 'AAA'], [(3, 5), 'AA'], 
 [(5, 8), 'AAA'], [(8, 11), 'IAA'], 
 [(11, 14), 'IAA'], [(14, 16), 'AA']]

或者,如果您希望它被分隔\n

list_of_strings = map(lambda l: '{}{}'.format(*l), result)
print('\n'.join(list_of_strings))

输出:

(0, 3)AAA
(3, 5)AA
(5, 8)AAA
(8, 11)IAA
(11, 14)IAA
(14, 16)AA

顺便说一句:不要string用作变量名,因为它可能与string模块冲突。

于 2013-05-27T23:56:49.777 回答