簡體   English   中英

Python:如何找出列表中句子的出現次數

[英]Python: how to find out the occurrences of a sentence in a list

我正在編寫一個函數來實現解決方案,以查找單詞列表中出現的單詞的次數,從文本文件中檢索,這非常簡單。

但是,我已經在它兩天試圖弄清楚如何檢查包含多個單詞的字符串的出現,可以是兩個或更多

例如,比如說字符串是:

"hello bye"

並且列表是:

["car", "hello","bye" ,"hello"]

該函數應返回值1因為元素“hello”和“bye”僅連續出現一次。


我最接近解決方案的是使用

words[0:2] = [' '.join(words[0:2])]

在給定索引的情況下將兩個元素連接在一起。 然而這是錯誤的,因為給定的輸入將是元素本身而不是索引。

有人能指出我正確的方向嗎?

將字符串與主列表中連續元素的連接匹配。 以下是示例代碼:

my_list = ["car", "hello","bye" ,"hello"]
sentence = "hello bye"
word_count = len(sentence.split())
c = 0

for i in range(len(my_list) - word_count + 1):
    if sentence == ' '.join(my_list[i:i+word_count]):
        c+=1

c持有的最終價值將是:

>>> c
1

如果您正在尋找單線 ,您可以使用zipsum作為:

>>> my_list = ["car", "hello","bye" ,"hello"]
>>> sentence = "hello bye"
>>> words = sentence.split()

>>> sum(1 for i in zip(*[my_list[j:] for j in range(len(words))]) if list(i) == words)
1

我們將這個問題分成兩部分。 首先,我們建立一個函數,它將返回給定列表的ngrams ,即n個連續元素的子列表:

def ngrams(l, n):
    return list(zip(*[l[i:] for i in range(n)]))

我們現在可以輕松獲得2,3或4克:

>>> ngrams(["car", "hello","bye" ,"hello"], 2)
[('car', 'hello'), ('hello', 'bye'), ('bye', 'hello')]
>>> ngrams(["car", "hello","bye" ,"hello"], 3)
[('car', 'hello', 'bye'), ('hello', 'bye', 'hello')]
>>> ngrams(["car", "hello","bye" ,"hello"], 4)
[('car', 'hello', 'bye', 'hello')]

每個項目都被制成一個元組。

現在將'hello bye'這個短語變成一個元組:

>>> as_tuple = tuple('hello bye'.split())
>>> as_tuple
('hello', 'bye')
>>> len(as_tuple)
2

由於這有2個單詞,我們需要從句子中生成雙字母組,並計算匹配的雙字母組的數量。 我們可以將這一切概括為

def ngrams(l, n):
    return list(zip(*[l[i:] for i in range(n)]))

def count_occurrences(sentence, phrase):
    phrase_as_tuple = tuple(phrase.split())
    sentence_ngrams = ngrams(sentence, len(phrase_as_tuple))
    return sentence_ngrams.count(phrase_as_tuple)

print(count_occurrences(["car", "hello","bye" ,"hello"], 'hello bye'))
# prints 1

兩種可能性。

## laboriously

lookFor = 'hello bye'
words = ["car", "hello","bye" ,"hello", 'tax', 'hello', 'horn', 'hello', 'bye']

strungOutWords = ' '.join(words)

count = 0
p = 0
while True:
    q = strungOutWords [p:].find(lookFor)
    if q == -1:
        break
    else:
        p = p + q + 1
        count += 1

print (count)

## using a regex

import re
print (len(re.compile(lookFor).findall(strungOutWords)))

我建議將問題減少到計算另一個字符串中字符串的出現次數。

words = ["hello", "bye", "hello", "car", "hello ", "bye me", "hello", "carpet", "shoplifter"]
sentence = "hello bye"
my_text = " %s " % " ".join([item for sublist in [x.split() for x in words] for item in sublist])


def count(sentence):
    my_sentence = " %s " % " ".join(sentence.split())
    return my_text.count(my_sentence)


print count("hello bye")
>>> 2
print count("pet shop")
>>> 0

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM