2016-11-29 4 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) 언급 목록)을 반환하지 않아야합니까? –

답변

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 [here] (http://stackoverflow.com/a/2402306/3025412)를 참조하십시오. – themistoklik

+0

개인적으로는 개인 목록에서 구두점을 구분하기 위해 처음부터 새 태그 목록에서 구두점을 필터링해야합니다. 몇 가지 언급 태그. – themistoklik

관련 문제