我想创建一个函数,当给定句子和字母时,该函数返回一个列表,其中包含该字母中句子中的所有单词。我想使用列表推导来返回列表。以下是预期的输出。
sentence = "Anyone who has never made a mistake has never tried
anything new"
words_containing(sentence, 'a')
['Anyone', 'has', 'made', 'a', 'mistake', 'has', 'anything']
关于如何进行这项工作的任何想法?
答案 0 :(得分:0)
def get_words (sentence, letter):
all_words_list = (sentence.split())
selected_words_list = [word for word in all_words_list if letter in word]
return selected_words_list
print (get_words ("Anyone who has never made a mistake has never tried anything new", "a"))
答案 1 :(得分:0)
def get_words (sentence, letter):
capital_letter = letter.capitalize()
all_words_list = (sentence.split())
selected_words_list = [word for word in all_words_list if letter in word or capital_letter in word]
return selected_words_list
print (get_words ("Anyone who has never made a mistake has never tried anything new", "a"))