Reputation: 91
I have a list of lists regarding tweets and I need to remove the username.
[['@Hegelbon','That','heart','sliding','into','the','waste','basket','.',':('],['“','@ketchBurning',':','I','hate','Japanese','call','him','"','bani','"',
':(',':(','”','Me','too'], ... ]
The main problem is that I don't know how to work with a list of lists. I tried the following code among other things but did not work:
import re
for element in tweets:
for word in element:
re.sub('@[^\s]+','', tweets)
Please help.
Upvotes: 1
Views: 173
Reputation: 318
Use list iterations:
mylist = [['@Hegelbon','That','heart','sliding','into','the','waste','basket','.',':('],['“','@ketchBurning',':','I','hate','Japanese','call','him','"','bani','"',
':(',':(','”','Me','too'] ]
newlist = [ [item for item in sublist if not item.startswith('@')] for sublist in mylist]
Upvotes: 1
Reputation: 106891
You can use a nested list comprehension to filter strings that starts with @
(assuming your list of lists is stored as variable l
):
[[i for i in s if not i.startswith('@')] for s in l]
This returns:
[['That', 'heart', 'sliding', 'into', 'the', 'waste', 'basket', '.', ':('], ['“', ':', 'I', 'hate', 'Japanese', 'call', 'him', '"', 'bani', '"', ':(', ':(', '”', 'Me', 'too']]
Upvotes: 2