사전이 필요하십니까?
텍스트가 길면 여러 항목에 대해 동일한 데이터를 여러 번 반복하는 데 많은 메모리가 필요합니다.
기능을 사용하는 경우 원하는 목록이 제공됩니다. 예 :
s = """In Newtonian physics, free fall is any motion
of a body where its weight is the only force acting
upon it. In the context of general relativity where
gravitation is reduced to a space-time curvature,
a body in free fall has no force acting on it and
it moves along a geodesic. The present article
concerns itself with free fall in the Newtonian domain."""
import re
def say_me(word,li=re.split('\s+',s)):
for i,w in enumerate(li):
if w==word:
print '\n%s at index %d followed by\n%s' % (w,i,li[i+1:])
say_me('free')
결과
free at index 3 followed by
['fall', 'is', 'any', 'motion', 'of', 'a', 'body', 'where', 'its', 'weight', 'is', 'the', 'only', 'force', 'acting', 'upon', 'it.', 'In', 'the', 'context', 'of', 'general', 'relativity', 'where', 'gravitation', 'is', 'reduced', 'to', 'a', 'space-time', 'curvature,', 'a', 'body', 'in', 'free', 'fall', 'has', 'no', 'force', 'acting', 'on', 'it', 'and', 'it', 'moves', 'along', 'a', 'geodesic.', 'The', 'present', 'article', 'concerns', 'itself', 'with', 'free', 'fall', 'in', 'the', 'Newtonian', 'domain.']
free at index 38 followed by
['fall', 'has', 'no', 'force', 'acting', 'on', 'it', 'and', 'it', 'moves', 'along', 'a', 'geodesic.', 'The', 'present', 'article', 'concerns', 'itself', 'with', 'free', 'fall', 'in', 'the', 'Newtonian', 'domain.']
free at index 58 followed by
['fall', 'in', 'the', 'Newtonian', 'domain.']
assignement li=re.split('\s+',s)
인수로서 전달 된 객체에 re.split('\s+',s)
파라미터 li
을 결합하는 방식이다.
이 바인딩은 한 번만 수행됩니다. 함수 정의를 해석기가 읽을 때 함수 개체를 만드는 순간입니다. 기본 인수로 정의 된 매개 변수입니다.
'defaultdict (list)'를 사용하면 파일을 읽고 각 행을 반복하는 방법을 알고 있다면 당신이 가고 싶은 곳으로 가야합니다. 더 구체적인 질문이 있으면 요청한 내용을 수정하십시오. – aelfric5578
"붙어. 아무것도 없어"- 정말? 전혀? 어딘가에서 (파일을 파싱 할 수있는 코드 작성) 시작하면 질문이보다 구체화되고 구체화됩니다. – stewSquared
내 코드가 도움이되는 것 같습니다. 고맙습니다. 나는 특별한 것은 없지만 당신의 질문을 업 그레 이드. 그러나 pythonista를 격려하는 것입니다! – eyquem