【发布时间】:2021-03-20 06:53:03
【问题描述】:
如果推文中存在用户名,我正在尝试找到从用户推文中删除推特用户名的最佳方法。例如,我有一个存储的推文数组,我想返回推文,并像这样取出用户名
tweets = ['@joe123 thank you', 'this reminds me of @john12', 'this tweet has no username tag in it']
clean_tweets = ['thank you', 'this reminds me of', 'this tweet has no username tag in it']
这是我目前所拥有的:
tweets = ['@joe123 thank you', 'this reminds me of @john12', 'this tweet has no username tag in it']
clean_tweets = [word for tweet in tweets for word in tweet.split() if not word.startswith('@')]
但是输出看起来像这样:
['thank',
'you',
'this',
'reminds',
'me',
'of',
'this',
'tweet',
'has',
'no',
'username',
'tag',
'in',
'it']
除了使用嵌套列表理解之外,我希望有更好的方法来解决这个问题。也许带有 lambda 的应用函数会更好地工作?有什么帮助谢谢
【问题讨论】:
标签: python string split data-cleaning