2016-11-29 100 views
2

我不認爲我的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('@')] 
+1

難道你不能只使用're.findall(r'\ B @ \ w +',tweet)'? https://regex101.com/r/jloffB/1 –

+0

爲什麼最後一個例子會返回一個包含空字符串的列表?它不應該返回一個空的列表---所有(零)提及的列表? –

回答

0

個人而言,我會跟在由Wiktor的意見提出了一個漂亮的正則表達式去,但如果你想避免它嘗試[tag[tag.index('@')+1:] for tag in tweet.split() if '@' in tag]

這樣做是什麼,如果它發現一個「@」在分割的令牌中,它會從@和下一個字母返回令牌。例如,如果tag='[email protected]那麼它將返回tag[2:]這是a123。

+0

但我想也刪除例如@ohio的標點符號!我怎麼能夠在函數調用中實現它? – vrrnki

+0

@jaqueline看到[這裏](http://stackoverflow.com/a/2402306/3025412) – themistoklik

+0

就我個人而言,我會分開我的關注點和過濾標點符號在新的標籤列表,而不是一開始,因爲你只需要清理幾個提及標籤。 – themistoklik