2016-11-29 6 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

関連する問題