2

I don't think my return statement passes all the test cases (the one with the empty string). The @FLOTUS is not a mention since a mention should proceed with a space or rather be the beginning of a tweet. So instead it should pass as an empty string. Any help would be appreciated on how to fix this!

def extract_mentions(tweet):
    ''' (str) -> list of str

Return a list containing all of the mentions in the tweet, in the order, they appear in the tweet.
Note: This definition of a mention doesn't allow for mentions embedded in other symbols.

Note: This definition of a mention doesn't allow for mentions embedded in other symbols.

>>> extract_mentions('@AndreaTantaros - You are a true journalistic professional. I so agree with what you say. Keep up the great work! #MakeAmericaGreatAgain')
['AndreaTantaros']
>>> extract_mentions('I'm joining @PhillyD tonight at 7:30 pm PDT / 10:30 pm EDT to provide commentary on tonight's #debate. Watch it here.')
['PhillyD']
>>> extract_mentions('Join me live in @Springfield, @ohio!')
['Springfield, ohio']
>>> extract_mentions('They endured beatings and jail time. They sacrificed their lives for this right@FLOTUS')
[''] '''

return [tag.strip('@') for tag in tweet.split() if tag.startswith('@')]
Rory Daulton
  • 21,934
  • 6
  • 42
  • 50
vrrnki
  • 23
  • 1
  • 4

1 Answers1

0

Personally I'd go with a nice regular expression as proposed in the comments by Wiktor, but if you want to avoid it try [tag[tag.index('@')+1:] for tag in tweet.split() if '@' in tag]

What this does is , if it finds a '@' character in the splitted tokens, it will return the token from the next letter of the @ and onwards. For example if tag='b@a123 then it will return tag[2:] which is a123.

themistoklik
  • 880
  • 1
  • 8
  • 19
  • but i wanted to also remove the punctuation for instance @ohio! How would i be able to implement that in the function call? – vrrnki Nov 29 '16 at 22:28
  • Personally I'd separate my concerns and filter for punctuation in the new tag list rather than in the beginning since you only need to clean a few mention tags. – themistoklik Nov 29 '16 at 22:39