Python字符串:如何解析字符串并找到特定的字符串索引?

2024-09-19 23:37:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我想创建一个元组列表,我想:

  • 元组的第一个元素=字母表的索引
  • 元组的第二个元素=下一个字母前的空白索引
# String
input= "M   i     n        d"

# List of tuple
output = [(0, 3), (4, 9), (10, 18), (19, 19)]

我能够编写这个逻辑(在最后一个元组中有一个错误),但我觉得必须有一种更聪明的方法来编写它。有什么想法吗

string = "M   i     n        d"
coltuple = []

for a in string:

    if a.isalpha() == True:
        start = string.index(a)
        next_string = string[(start + 1) :]

        if next_string:

            for b in next_string:

                if b.isalpha() == True:
                    end = string.index(b) - 1
                    print("End:", end)
                    break
        else:
            end = len(string) - 1

        coltuple += [(start, end)]

print(coltuple)

Tags: intrue元素列表forstringindexif
2条回答

这可以通过使用re模块来解决

import re

L = []
string = "M   i     n        d"

pat = re.compile(r'\S+\s*')

for token in pat.finditer(string):
    L.append((token.start(), token.end()-1))

print(L)

印刷品:

[(0, 3), (4, 9), (10, 18), (19, 19)]

如果要使用这些值索引到字符串中,最好使用token.end()而不是token.end()-1

注意:从常规表达式中删除了捕获括号。它是r'(\S+\s*)

这就是我想到的:

inputString= "M   i     n        d"

alphaIndexes = []
alphaTuples = []

# Loop over range based on length of input
for i in range(0, len(inputString)):
    # if its alpha
    if inputString[i].isalpha() == True:
        print("Alpha at {}".format(i))
        # append it to list of indexes
        alphaIndexes.append(i)

# Loop over range based on length of all found alphas
# minus one since we will create pairs
for i in range(0, len(alphaIndexes)-1):
    # Append to list o alpha tuples tuple of
    # current index and next index but substract that next one by one
    alphaTuples.append((alphaIndexes[i], alphaIndexes[i+1]-1))

print(alphaTuples)

相关问题 更多 >