在Python中,可以通过以下几种方式筛选出想要的词汇:
words = ["apple", "banana", "cherry", "durian"]
filtered_words = []
for word in words:
if "a" in word:
filtered_words.append(word)
print(filtered_words) # 输出:['apple', 'banana']
words = ["apple", "banana", "cherry", "durian"]
filtered_words = [word for word in words if "a" in word]
print(filtered_words) # 输出:['apple', 'banana']
words = ["apple", "banana", "cherry", "durian"]
filtered_words = list(filter(lambda word: "a" in word, words))
print(filtered_words) # 输出:['apple', 'banana']
以上三种方法都可以根据具体的需求来进行筛选出想要的词汇。可以根据实际情况选择最适合的方式。
辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读: python怎么定义numpy数组