[英]Check which words from a list could be formed with a string of letters
System: Mac Python 2.7 系统:Mac Python 2.7
Hi, So I have a list that contains words from the English dictionary I found online. 嗨,所以我有一个列表 ,其中包含我在网上找到的英语词典中的单词。 Next, I have a string of lowercase letters .
接下来,我有一串 小写字母 。 What I want to do is find all the words in the list (English dictionary) that are made of letters in the string and save them to a separate list.
我想做的是找到列表(英语词典)中所有由字符串中的字母组成的单词,并将它们保存到单独的列表中。 Also, I do not want to be able to reuse letters (glue -> glee) but the letters given can be rearranged.
另外,我也不想重复使用字母(胶水->欢乐合唱团),但是给定的字母可以重新排列。 An example string could be "hideg" and the result should generate a list containing:
示例字符串可以是“ hideg”,结果应生成一个包含以下内容的列表:
['hide','hid','hie','dig','eh','he','hi','die','ed','id','i']
Here is what I have got so far (Note: myWord is the list (english dictionary) and myLetters is the string): 这是到目前为止我得到的(注意:myWord是列表(英语词典),myLetters是字符串):
def validWord(myWord, myLetters):
myLetters = "".join(sorted(myLetters))
matches = []
for i in myWord:
if "".join(sorted(i)) in myLetters:
matches.append(i)
return matches
The problem in your code is that you are comparing exact match, which in most cases the letters are not fully used, ie. 您的代码中的问题是,您正在比较完全匹配,在大多数情况下,即完全不使用字母。 hide != hideg, but of course it can form the word.
hide!= hideg,但是它当然可以构成单词。
One easy way (although not optimized) is to make use of collections.Counter , like this: 一种简单的方法(尽管未优化)是利用collections.Counter ,如下所示:
In [32]: from collections import Counter
In [33]: words = ['hide','hid','hie','dig','eh','he','hi','die','ed','id','i']
In [34]: letters = 'hide'
In [35]: def valid_word(words, letters):
....: results = []
# looping through words list and do a comparison
....: for word in words:
# build a letter counter for letters
....: letter_counter = Counter(letters)
# subtract their letter count, -ve means cannot form the word
....: letter_counter.subtract(Counter(word))
....: if any(c < 0 for c in letter_counter.values()):
....: continue
# only append the result if >=0 letters left in counter
....: results.append(word)
....: return results
....:
Usage: 用法:
In [36]: valid_word(words, letters)
Out[36]: ['hide', 'hid', 'hie', 'eh', 'he', 'hi', 'die', 'ed', 'id', 'i']
In [37]: valid_word(words, 'hideg')
Out[37]: ['hide', 'hid', 'hie', 'dig', 'eh', 'he', 'hi', 'die', 'ed', 'id', 'i']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.