【发布时间】:2023-03-30 11:33:01
【问题描述】:
我不确定是否还有其他相关问题。如果是这样,请告诉我...我已经搜索过了,但我找不到任何...
如果某些单词的前面没有三个或更少的单词,我想计算单词列表。 这是来自Count occurrences of a couple of specific words的示例
我想计算单词,“foo”、“bar”、“baz”,除了“no”,在单词前面三个或更少的单词。在这种情况下,一个 bar 和 foo 不能被计算在内..
vocab = ["foo", "bar", "baz"]
exception= ["no"]
s = "foo bar baz no bar quux foo bla bla"
wordcount = dict((x,0) for x in vocab)
for w in re.findall(r"\w+", s):
if w in wordcount:
wordcount[w] += 1
请帮助我.. 非常感谢你提前..
【问题讨论】: