我正在尝试从字符串中删除所有提及任何人的内容,我想知道是否有更快的方法来做到这一点?
text = "hey @foo say hi to @bar"
textsplit = text.split()
n = -1
ts2 = textsplit
for x in textsplit:
n += 1
if x[0]== "@":
del ts2[n]
text = ' '.join(ts2)
提前致谢。 (这有点像 Removing elements from a list containing specific characters,但这个有点不同。)
请您参考如下方法:
这与您的代码相同:
' '.join(x for x in text.split() if not x.startswith('@'))
