将输入与列表进行比较,以查看单词中是否有单词



将获取输入的前两个字母并将其与列表中的每个单词进行比较如果输入的前两个字母出现在列表中的任何单词中,它将追加到另一个列表

我没有尝试任何东西,因为我想到底是怎么回事,请帮助

由于您没有提供任何编程语言,但在另一个问题中使用了Python,所以我将用Python提供我的答案:

words = ["Hello", "World", "Foo", "Bar", "Baz"]
def beginsWith(word, letters):
return word[:len(letters)] == letters
result = []
letters = "Ba"
for word in words:
if beginsWith(word, letters):
result.append(word)
print(result) # Expected: ["Bar", "Baz"]

最新更新