从 Python 中的列表动态连接字符串
Dynamically Concatenating String from a List in Python
我想使用 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
您可以使用 itertools.groupby
、理解和 str.join
:
stringList = ["One", "Two", "Three", "456", "Seven", "Eight", "Nine"]
from itertools import groupby
[''.join(g) for k,g in groupby(stringList, lambda x: x[0].isdigit())]
输出:
['OneTwoThree', '456', 'SevenEightNine']
工作原理:
groupby
将连续的值分组,这里我对第一个字符进行了测试,以检测它是否为数字。所以所有连续的字符串都连接在一起。
如果格式更适合您,作为字典:
dict(enumerate(''.join(g) for k,g in groupby(stringList,
lambda x: x[0].isdigit())))
输出:
{0: 'OneTwoThree', 1: '456', 2: 'SevenEightNine'}
我不想加入连续号!
然后您可以将以上内容与对组身份的测试结合起来(如果字符串以数字开头则为真)并使用 itertools.chain
链接输出:
stringList = ["One", "Two", "Three", "456", "789", "Seven", "Eight", "Nine"]
from itertools import groupby, chain
list(chain(*(list(g) if k else [''.join(g)]
for k,g in groupby(stringList, lambda x: x[0].isdigit()))))
输出:
['OneTwoThree', '456', '789', 'SevenEightNine']
我想使用 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
您可以使用 itertools.groupby
、理解和 str.join
:
stringList = ["One", "Two", "Three", "456", "Seven", "Eight", "Nine"]
from itertools import groupby
[''.join(g) for k,g in groupby(stringList, lambda x: x[0].isdigit())]
输出:
['OneTwoThree', '456', 'SevenEightNine']
工作原理:
groupby
将连续的值分组,这里我对第一个字符进行了测试,以检测它是否为数字。所以所有连续的字符串都连接在一起。
如果格式更适合您,作为字典:
dict(enumerate(''.join(g) for k,g in groupby(stringList,
lambda x: x[0].isdigit())))
输出:
{0: 'OneTwoThree', 1: '456', 2: 'SevenEightNine'}
我不想加入连续号!
然后您可以将以上内容与对组身份的测试结合起来(如果字符串以数字开头则为真)并使用 itertools.chain
链接输出:
stringList = ["One", "Two", "Three", "456", "789", "Seven", "Eight", "Nine"]
from itertools import groupby, chain
list(chain(*(list(g) if k else [''.join(g)]
for k,g in groupby(stringList, lambda x: x[0].isdigit()))))
输出:
['OneTwoThree', '456', '789', 'SevenEightNine']