从另一个列表中的列表中查找字符串对象

时间:2016-07-06 13:49:52

标签: python python-2.7 if-statement for-loop

我正在做一个小项目,我的程序解读一个字符串并找到它的每个可能的组合。

我有两个清单; comboListwordListcomboList拥有该词的每个组合;例如,comboList的{​​{1}}是:

'ABC'

(只有['ABC','ACB','BAC','BCA','CAB','CBA'] 才是真正的词)

'CAB'包含从文本文件导入的约56,000个单词。这些都可以在英语词典中找到,并按长度排序,然后按字母顺序排列。

wordList是我的功能,通过检查isRealWord(comboList,wordList)中是否有comboList来测试wordList中的哪些字是真实的。这是代码:

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'

这是输出:

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 wordList没有返回True,我该如何解决?

2 个答案:

答案 0 :(得分:2)

我建议使用--- pdf_document: default output: pdf_document --- ```{r setup, include=FALSE} knitr::opts_chunk$set(echo = TRUE) ``` ```{r results='asis'} library(xtable) options(xtable.comment = FALSE) df <- data.frame(cbind(c("a", "b"), c("$\\sin\\theta$", "$\\cos\\theta$"))) print(xtable(df),type="latex", hline.after = NULL, sanitize.text.function=function(x){x}) ``` ,因为它的实现速度会快得多。有set方法。这是一个不区分大小写的解决方案:

set.intersection

答案 1 :(得分:1)

我认为这里的问题是你用相同的字母比较两个字符串,但混合低/大写
要查看我是否正确,请尝试将wordList中的所有字词转换为大写字母,然后再将isRealWord与大写字词进行比较(只是为了确定),如下所示:

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)
相关问题