Python:计算单词列表,除非某些单词在前面

2024-09-29 21:28:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我不确定是否还有其他相关问题。如果是,请告诉我。。。我已经找过了,但找不到。。你知道吗

我想数一数单词表,如果某些单词前面没有三个或更少的单词。。 下面是来自Count occurrences of a couple of specific words的示例

我想数单词,“foo”,“bar”,“baz”,除了“no”在单词前面三个或更少的单词。在这种情况下,一个吧台和一个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

请帮帮我。。事先非常感谢。。你知道吗


Tags: ofnoinforfoocountbarbaz
3条回答

只需用空字符串替换no和以下三个单词,然后计算结果字符串中的单词。你知道吗

>>> s = 'foo bar baz no bar quux foo bla bla'
>>> vocab = ["foo", "bar", "baz"]
>>> exception= ["no"]
>>> wordcount = dict((x,0) for x in vocab)
>>> m = re.sub(r'(?:^|\s)no(\s+\S+){0,3}', '', s)
>>> for w in re.findall(r"\w+", m):
        if w in wordcount:
            wordcount[w] += 1


>>> wordcount
{'foo': 1, 'bar': 1, 'baz': 1}

实际上,您可以使用Python的字符串来实现这一点,而不需要regex:

vocab = ["foo", "bar", "baz"]
ex_list= ["no"]
s = "foo bar baz no bar quux foo bla bla"

words=s.split()
wordcount = dict((x,0) for x in vocab)
for i, word in enumerate(words):
    if i>=3 and any(w in ex_list for w in words[i-3:i]):
        continue
    elif word in vocab:    
        wordcount[word]+=1

由于切片不会生成索引错误,因此可以将循环简化为:

for i, word in enumerate(words):
    if word in vocab and not any(w in ex_list for w in words[i-3:i]):
        wordcount[word]+=1

关于:

vocab = ["foo", "bar", "baz"]
exception= ["no"]
s = "foo bar baz no bar quux foo bla bla"

wordcount = dict((x,0) for x in vocab)

words = s.split()

i = 0
while i < len(words):
    cur_word = words[i]
    if cur_word in exception:
        i += 4
    else:
        if cur_word in vocab: wordcount[cur_word] += 1
        i += 1

print wordcount  # {'baz': 1, 'foo': 1, 'bar': 1}

这只是利用了一个事实,如果我们遇到“否”,我们可以跳过以下3个元素。你知道吗

相关问题 更多 >

    热门问题