我想不出一个题目,但我可以解释。假设用户必须输入一个包含两个单词的字符串。然而,有多种方法来书写每一个单词。比如:
words = input("Enter 'Hello World' however you want to type it ")
possibilities = [
"Hello World",
"hello World",
"Hello world",
"hello world",
"Helo World",
"helo World",
"Helo world",
"helo world"
]
if words in possibilities:
print("true")
有没有办法这样做
words = input("Enter 'Hello World' however you want to type it ")
firstWord = [
"Hello",
"hello",
"Helo",
"helo"
]
secondWord = [
"World",
"world",
]
words = words.split(' ')
if words[0] in firstWord and words[1] in secondWord:
print("true")
回答问题时,可以使用
itertools.product
生成两个列表的所有组合这可以扩展到:
使用Levenshtein distance
请参见https://tedboy.github.io/nlps/generated/generated/nltk.edit_distance.html#nltk-edit-distance作为python库的示例,该库将计算“Hello”和“helo”之间的距离
相关问题 更多 >
编程相关推荐