简体   繁体   English

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

[英]Python String: How to parse string and find specific String index?

I want to create a list of tuples, where I want:我想在我想要的地方创建一个元组列表:

  • first element of tuple = index of alphabet元组的第一个元素 = 字母索引
  • second element of tuple = index of whitespace before the next alphabet元组的第二个元素 = 下一个字母前的空格索引
# String
input= "M   i     n        d"

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

I was able to write this logic(with an error in the last tuple), but feel that there must be a smarter way of writing this.我能够编写这个逻辑(在最后一个元组中有错误),但觉得必须有一种更聪明的方式来编写它。 Any idea?任何的想法?

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)

This could be solved using the re module.这可以使用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)

Prints:印刷:

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

If you are going to use these values to index into the string, you might be better off using token.end() rather than token.end()-1 .如果您打算使用这些值索引到字符串中,最好使用token.end()而不是token.end()-1

Note: removed capturing parentheses from the regular exp.注意:从常规 exp 中删除了捕获括号。 It was r'(\\S+\\s*)它是r'(\\S+\\s*)

This is what I came up with:这就是我想出的:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM