gpt4 book ai didi

python - 如何检查列表中的单词是否至少包含一定数量的单个字母

转载 作者:太空宇宙 更新时间:2023-11-04 07:14:34 25 4
gpt4 key购买 nike

在 Python 中,我想知道一个列表是否包含一个项目以及有多少。

例如,在:

sentence = "Paul sat in a tree and watched seven squirrels playing on the ground." 

我希望能够只提取包含两个 e 的单词,打印出来

["tree", "seven"].

目前我有:

[x for x in sentence.split() if "ee" in x]

但这只会输出 ["Tree"] 因为我假设它只能挑选出两个 e 直接相邻的单词。

我该怎么做才能覆盖元素中的所有 e,而不管它们位于何处?

最佳答案

您可以使用 count获取字符串中元素的个数

In [1]: data = "Paul sat in a tree and watched seven squirrels playing on the ground."

In [2]: [x for x in data.split() if x.count('e') > 1]
Out[2]: ['tree', 'seven']

关于python - 如何检查列表中的单词是否至少包含一定数量的单个字母,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52717201/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com