2014-06-19 4 views
0

텍스트 문자열에서 목록의 목록을 생성하는 함수가 있습니다.목록 목록에 숨겨진 단어 Python

  • row_start 단어의 첫 번째 문자의 행 번호 : 나는 그것을 행 또는 열을 기준으로하는지 여부, 목록의 목록 내에서 단어 즉 "느와르"를 발견하고 그 단어의 좌표를 반환하고 싶습니다 .
  • column_start은 단어의 첫 글자에 대한 열 번호입니다.
  • row_end은 단어의 마지막 글자에 대한 줄 번호입니다.
  • column_end은 단어의 마지막 글자에 대한 줄 번호입니다.

여기까지 제 코드입니다. 상기 기능

def checkio(text, word): 
    rows = [] 
    col = [] 
    coordinates = [] 
    word = word.lower() 
    text = text.lower() 
    text = text.replace(" ", "") 
    text = text.split("\n") 
    for item in text: 
     rows.append([item]) #Creates a list of lists by appending each item in brackets to list. 

출력 예 : 위의 경우

[['hetookhisvorpalswordinhand:'], 
    ['longtimethemanxomefoehesought--'], 
    ['sorestedhebythetumtumtree,'], 
    ['andstoodawhilei**n**thought.'], 
    ['andasinuffishth**o**ughthestood,'], 
    ['thejabberwock,w**i**theyesofflame,'], 
    ['camewhifflingth**r**oughthetulgeywood,'], 
    ['andburbledasitcame!']] 

될 것 인 "노아"인 좌표 [4, 16, 7, 16]. 행 시작 행 번호를 4 칼럼 시작 열 번호를 16 로우 엔드 행 # 7 이고 컬럼 단부 단어가 수평 및 수직으로 볼 수

단어 되돌릴 수없는 16 컬럼 번호이다

+0

예제 목록과 예제 출력을 추가하면 도움이됩니다. – timgeb

+0

또한 숨겨진 단어를 목록의 목록에 여러 번 나타날 수 있습니까? 그렇다면 모두를 찾거나 처음 만 찾으면 되나요? – timgeb

+0

row_end를 원하면 단어를 세로로 찾고 있습니까? (단어 검색 게임 에서처럼?) 단어를 바꿀 수 있습니까? 목록 목록이 사각형입니까 (별 상관 없음)? – brechmos

답변

0

꽤 완벽하지는 않지만 질문에 대답합니다. :-) 나는 이것을 문자열의 목록으로 만들었습니다. 이전에 코드에서 완료해야합니다.

words = [ 
    'hetookhisvorpalswordinhand:', 
    'longtimethemanxomefoehesought--', 
    'sorestedhebythetumtumtree,', 
    'andstoodawhileinthought.', 
    'andasinuffishthoughthestood,', 
    'thejabberwock,witheyesofflame,', 
    'camewhifflingthroughthetulgeywood,', 
    'andburbledasitcame!' 
] 

word = 'noir' 

print [[row+1, line.find(word)+1, row+1, line.find(word)+len(word), line] for row, line in enumerate(words) if line.find(word) >= 0] 

words_transp = [''.join(t) for t in zip(*words)] 

print [[line.find(word)+1, col+1, line.find(word)+len(word), col+1, line] for col, line in enumerate(words_transp) if line.find(word) >= 0] 

출력은 다음과 같습니다

[[4, 16, 7, 16, 'sotnoira']] 

주, 오류 점검하지 많은. OP를위한 운동. :-)

아직, 파이썬이 0에서 시작하므로 카운트에주의를 기울여야합니다. 따라서 "+1"이 거기에 있습니다.