【发布时间】:2022-01-04 14:45:54
【问题描述】:
我想使用 python 动态连接列表中包含的字符串,但我的逻辑遇到了错误。
目标是连接字符串,直到找到以数字开头的字符串,然后将这个数字字符串隔离到它自己的变量中,然后将剩余的字符串隔离到第三个变量中。
例如:
stringList = ["One", "Two", "Three", "456", "Seven", "Eight", "Nine"]
resultOne = "OneTwoThree"
resultTwo = "456"
resultThree = "SevenEightNine"
这是我尝试过的:
stringList = ["One", "Two", "Three", "456", "Seven", "Eight", "Nine"]
i = 0
stringOne = ""
stringTwo = ""
stringThree = ""
refStart = 1
for item in stringList:
if stringList[i].isdigit() == False:
stringOne += stringList[i]
i += 1
print(stringOne)
elif stringList[i].isdigit == True:
stringTwo += stringList[i]
i += 1
print(stringTwo)
refStart += i
else:
for stringList[refStart] in stringList:
stringThree += stringList[refStart]
refStart + 1 += i
print(stringThree)
它会出错并显示以下消息:
File "c:\folder\Python\Scripts\test.py", line 19
refStart + 1 += i
^
SyntaxError: 'operator' is an illegal expression for augmented assignment
【问题讨论】:
-
我没有看到“refStart + 1 += i”部分代码
-
哦,我的错。我忘了我在提交之前已经编辑过了
标签: python list concatenation