我不認爲我的return語句通過了所有的測試用例(空字符串)。 @FLOTUS不在話下,因爲提到的應該是一個空間,或者說是推文的開始。所以相反,它應該作爲一個空字符串傳遞。任何幫助將不勝感激如何解決這個問題!從列表中提取元素?
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 [email protected]')
[''] '''
return [tag.strip('@') for tag in tweet.split() if tag.startswith('@')]
難道你不能只使用're.findall(r'\ B @ \ w +',tweet)'? https://regex101.com/r/jloffB/1 –
爲什麼最後一個例子會返回一個包含空字符串的列表?它不應該返回一個空的列表---所有(零)提及的列表? –