作者:倩女__沃洪伟 | 来源:互联网 | 2022-12-02 17:53
在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,无论它们位于何处?
1> Praveen..:
您可以使用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']