你能在python中使用for x搜索文本组吗



所以我正在开发一个小程序,它可以扫描一行压缩文本(想想变量1会缩短为V1,变量2会缩短为V2,像Start这样的东西会缩短为S(。我正试图让我的程序使用for x…循环扫描此文本。我提前对所有评论表示歉意,请随时修改!无论如何,这是我的代码:

Text = "S V1XV2E" # The code is incomplete to search all the text, but that's a me problem
I = 0 # Create index for later
for x in Text: # Look through all text
I = I + 1 # Increase index for every letter searched
if x == Text[:I]: # Text starts at 0, Text[:I] = 0th char always = S 
if x == "S": # Therefore this is always correct unless the sample changes
print("Start")
I = 1 # Set "I" to 1 to skip over the space
if x == Text[I:I+2]: # Here the idea is since "I" is set to 1, it will search the text from the 1st to 3rd char, so "V1"
if x == "V1": # "x" is only 1 character, so this always fails
print("Var 1")

唯一的问题是;x〃;在这种情况下,一次只能看到一个字符。我似乎找不到任何其他方法来让它代表多个角色。有更好的方法吗?

非常感谢在这个问题上提供的任何帮助!

假设压缩文本中的变量名称总是只有一个字母,它总是在名称的开头,并且可以忽略空白,那么可以使用以下函数:

def vnSplit(cText):
#just in case:
cText = str(cText) 
if cText == '':
return []
#remove whitespace
cText = ''.join(cText.split())
varNames = []
varName = cText[0]
ctLen = len(cText)
for i in range(1, ctLen):
if cText[i].isalpha():
varNames.append(varName)
varName = '' 

if i == ctLen - 1: #last char
varNames.append(varName + cText[i])
else: 
varName = varName + cText[i]

return varNames

然后你可以像for x in vnSplit(Text):...一样循环通过

以下是函数的一些示例输出:

("S V1XV2E") ---> ['S', 'V1', 'X', 'V2', 'E']
("SV2Exyz") ---> ['S', 'V2', 'E', 'x', 'y', 'z']
("abc324k_1") ---> ['a', 'b', 'c324', 'k_1']

[使用for ct in cTexts: print(f'("{ct}") ---> {vnSplit(ct)}')]打印

编辑:如果你想用字典而不是单字母格式,你可以做一些事情,比如:

def startVar(string, vDict):
possVars = [v for v in vDict if string.startswith(v)]
return possVars[0] if possVars else False
def vnExtract(cText, vDict): 
cText = str(cText) #just in case
#remove whitespace - but not on original
ct = ''.join(cText.split())
compressedVars = [] 
extractedVars = []
varName = ''
varSuffix = ''
while len(ct) > 0: 
possVar = startVar(ct, vDict)
if possVar:
if varName != '': 
compressedVars.append(varName + varSuffix)
if varSuffix != '' and varSuffix[0].isalpha(): #OPTIONAL
varSuffix = '_' + varSuffix #OPTIONAL
extractedVars.append(vDict[varName] + varSuffix)
varName = possVar
varSuffix = ''
ct = ct.replace(varName, '', 1)
else:
varSuffix = varSuffix + ct[0]
ct = ct[1:]

if ct == '' and varName in vDict:
compressedVars.append(varName + varSuffix)
if varSuffix != '' and varSuffix[0].isalpha(): #OPTIONAL
varSuffix = '_' + varSuffix #OPTIONAL
extractedVars.append(vDict[varName] + varSuffix)

return {
'compressed_text': cText,
'compressed_vars': compressedVars,
'extracted_vars': extractedVars
}

它的调用方式相同,但取决于startVar(),并且输出格式不同(通过编辑return很容易更改(;但这样一来,压缩必须遵守字典

最新更新