问题描述
我知道以前已经询问过这种方法的变体,但是我无法理解任何以前的实现,因为它们大多数涉及使用集和issubset方法。
这是我要尝试的操作:我在词典中有一组单词和可能的字母列表。我想确定是否可以通过重新排列列表中的字母来组成集合的成员。这是我当前的实现:
def solve(dictionary,letters):
for word in dictionary: #for each word in the dictionary
if len(word) > len(letters): # first optimization,doesn\'t check words that are larger than letter set
continue
else:
scrambledword = \"\".join([b for b in sorted(list(word))]) #sorts the letters in each word
if set(scrambledword).issubset(letters):
print word
def main():
dictionary = set([x.strip() for x in open(\"C:\\\\Python27\\\\dictionary.txt\")])
letters = sorted([\'v\',\'r\',\'o\',\'m\',\'a\',\'b\',\'c\',\'d\'])
solve(dictionary,letters)
main()
此实现的明显问题是,会发现某些单词在“字母”中使用多个字母。例如,单词“'cardboard \'显示为有效单词,尽管只有\\的一个副本。字母列表中的“ a \”和\“ r \”。如何在列表上使用\“ issubset \”方法?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)