2016-07-06 3 views
1

내 프로그램이 문자열을 해독하고 가능한 모든 조합을 찾는 작은 프로젝트를 수행하고 있습니다.다른 목록의 목록에서 문자열 개체 찾기

두 개의 목록이 있습니다. comboListwordList. comboList은 단어의 모든 조합을 보유합니다. 예를 들어, 'ABC'에 대한 comboList은 다음과 같습니다

['ABC','ACB','BAC','BCA','CAB','CBA'] 

wordList 텍스트 파일에서 가져온 약 56,000 단어를 보유 (만 'CAB' 진짜 단어입니다). 이것들은 모두 영어 사전에서 찾아 길이별로 정렬 된 다음 알파벳 순으로 정렬됩니다.

isRealWord(comboList,wordList)comboList에있는 어떤 단어가 wordList에 있는지 확인하여 실제로 테스트하는 기능입니다.

run c:/Users/uzair/Documents/Programming/Python/unscramble.py 
Please give a string of scrambled letters to unscramble: abc 
['A', 'B', 'C'] 
['ABC', 'ACB', 'BAC', 'BCA', 'CAB', 'CBA'] 
Loading word list... 
55909 words loaded 
Debug 1 
Debug 2 
Debug 2 
Debug 2 
Debug 2 
Debug 2 
Debug 2 
Debug 4 
[] 

if combo in wordListTrue을 반환하지 않습니다이며 어떻게 해결합니까 :

def isRealWord(comboList, wordList): 
    print 'Debug 1' 
    for combo in comboList: 
     print 'Debug 2' 
     if combo in wordList: 
      print 'Debug 3' 
      print combo 
      listOfActualWords.append(combo) 
    print 'Debug 4' 

이 출력은 : 여기 코드는?

+6

단어 목록도 모두 대문자입니까? – L3viathan

+2

isRealWord 메서드 내에서 디버그 2 행을 >> print "{0} : {1}"형식으로 바꿀 수 있으며 비교 대상을 정확히 볼 수 있습니다. - 괜찮은 디버깅 - wordList는 단어 톤입니다. ..하지만 귀하의 콤보를 인쇄하여 정확히 무엇을 확인하는지보십시오! – AK47

+0

사이드 노트 :'set'을 사용하여'wordList'를 유지하는 것이 훨씬 빠릅니다! 'wordList'가 코드를 포함하고있는'list' 일 필요가있을 때마다 매번 isRealWord 안에'set'을 만들 가치가 있습니다. – MisterMiyagi

답변

1

여기서 문제는 두 글자를 같은 글자로 비교하는 것이지만 대/소문자 인을 섞어서 사용하는 것입니다.

UpperCaseWordList = [word.upper() for word in wordList] 
... 
def isRealWord(comboList, wordList): 
    for combo.upper() in comboList: 
     if combo in wordList: 
      print combo 
      listOfActualWords.append(combo) 
+0

'wordList' 매개 변수를'UpperCaseWordList'로 변경해야한다고 생각합니다. –

+0

'wordList'는 이제 메소드의 인자입니다 - 그래서 호출은 다음과 같을 것입니다 :'isRealWord (comboList, UpperCaseWordList)' –

2

나는 좋을 것 : 내가 올바른 해요 경우
는 대문자로 wordList에있는 모든 단어를 변환하려고 후 다음과 같이 isRealWord에 (단지 확인하기 위해) 대문자 단어와 비교 확인하려면 set을 사용하면 구현이 빨라지기 때문에 훨씬 빠릅니다. set.intersection 방법이 있습니다. 대소 문자를 구별하지 않는 솔루션입니다.

listOfActualWords = set.intersection(set(word.upper() for word in comboList), set(word.upper() for word in wordList)) 
관련 문제