mylist = ['Arthur Morgan Game Name: Red Dead Redemption2',
'Zealda',
'Geralt Game Name:Witcher3','Uncharted4']
我上面有一个字符串列表,我想先按“游戏名称:”进行分割,而只在分隔符之后保留文字。我尝试了[w.split('Game Name: ') for w in mylist]
下一步是什么?
预期结果:
['Red Dead Redemption2','Zealda','Witcher3','Uncharted4']
谢谢!
答案 0 :(得分:1)
[w.split('Game Name:')[-1] for w in mylist]
编辑,根据Martijn Pieters partition()
,即使您需要检查空字符串,速度也更快。
[w.partition('Game Name:')[-1] or w for w in mylist]
答案 1 :(得分:-1)
mylist = ['Arthur Morgan Game Name: Red Dead Redemption2',
'Zealda',
'Geralt Game Name:Witcher3','Uncharted4']
gameNames = [w.split(":")[-1].strip(" ") for w in mylist]
print(gameNames)
返回:
['Red Dead Redemption2', 'Zealda', 'Witcher3', 'Uncharted4']