我已经构建了一个程序,它随机生成8个单独的字母,并将它们分配到一个名为ranlet的列表中(随机字母的缩写).然后它将.txt文件导入名为wordslist的列表中.随机生成字母和加载文件都很好,因为我已经单独测试了这些部分,但后来我遇到了麻烦.
然后,程序必须将ranlet列表与单词列表列表进行比较,将匹配的单词追加到名为hits的列表中,并在hits列表中显示单词
我试过这个:
for each in wordslist:
if ranlet==char in wordslist:
hits.append(wordslist)
else:
print "No hits."
print hits
可悲的是,这没有用.我对此有很多变化,但都无济于事.我真的很感激有关此事的任何帮助.
解决方法:
我想你可以从set.intersection受益:
set_ranlet = set(ranlet)
for word in word_list:
intersection = set_ranlet.intersection(word)
if intersection:
print "word contains at least 1 character in ran_let",intersection
#The following is the same as `all( x in set_ranlet for x in word)`
#it is also the same as `len(intersection) == len(set_ranlet)` which might
# be faster, but less explicit.
if intersection == set_ranlet:
print "word contains all characters in ran_let"