在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,无论它们位于何处?
答案 0 :(得分:8)
您可以使用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']