我正在尝试制作一个可以生成三个随机字母的程序。然后,用户必须键入包含这些字母的单词,并且是字典中包含的有效单词。但我遇到的问题是,我不确定如何检查用户输入的单词是否包含随机生成的三个字母。然后根据字典部分进行检查很简单。
答案 0 :(得分:1)
if(userStr.contains("rl1") && userStr.contains("rl2") && userStr.contains("rl3")) {
//user string contains random letter
}
答案 1 :(得分:0)
您可以使用import nltk
from nltk.tokenize import word_tokenize
def end_of_line():
filename = raw_input("Please enter a text file.> ")
with open(filename) as f:
for line in f:
linewords = nltk.tokenize.word_tokenize(line)
lw_tagged = nltk.tag.pos_tag(linewords)
last_lw_tagged = lw_tagged.pop()
for (word, tag) in last_lw_tagged:
if tag == "NN":
print line
end_of_line()
Traceback (most recent call last):
File "/private/var/folders/ly/n5ph6rcx47q8zz_j4pcj3b880000gn/T/Cleanup At Startup/endofline-477697124.590.py", line 15, in <module>
end_of_line()
File "/private/var/folders/ly/n5ph6rcx47q8zz_j4pcj3b880000gn/T/Cleanup At Startup/endofline-477697124.590.py", line 11, in end_of_line
for (word, tag) in last_lw_tagged:
ValueError: too many values to unpack
logout
查找该单词是否包含该字母。
如果char存在于string中,则返回索引(&gt; = 0)。如果没有,它 返回-1。因此,非负返回值意味着char存在 在这个词中。
indexOf(char)