如何检查一个术语在列表中出现的次数及其频率?
Posted
技术标签:
【中文标题】如何检查一个术语在列表中出现的次数及其频率?【英文标题】:How do I check how many times a term has occured in a list and its frequency? 【发布时间】:2022-01-14 03:08:39 【问题描述】:text=["duran duran sang wild boys in 1984","wild boys don't remain forever wild","who brought wild flowers","it was john krakauer who wrote in to the wild"]
例如在此列表中"duran"
出现在 1 个句子中,并且在 text[0]
中出现了 2 次p>
对于单独的代码,第一个输出应该是1
,第二个输出应该是2
我尝试了 .count
和 .check
的函数,但无法生成正确的代码。
【问题讨论】:
“对于单独的代码,第一个输出应该是 1,第二个输出应该是 2”是什么意思? @np8 有单独的代码第一个代码应该输出单词在多少个句子中(例如:duran 在 1 个句子中)第二个代码应该输出单词在句子中的频率(例如:第二个代码收集第一句中 duran 的信息,并检查该句子中有多少“duran”,即 2) 【参考方案1】:此代码计算word
在列表l
列表中的所有字符串中出现的所有次数,并返回包含此word
的列表元素的数量。
text=["duran duran sang wild boys in 1984","wild boys don't remain forever wild","who brought wild flowers","it was john krakauer who wrote in to the wild"]
def countInListOfLists(l, word):
counts = [s.count(word) for s in l]
return sum([1 for c in counts if c > 0]), sum(counts)
print(countInListOfLists(text, "duran"))
输出:
(1,2)
【讨论】:
我如何让它重复这个输出? def get_unique_words(a):visited = set() uniq = [] for word in a.split(): if word not invisited: uniq.append(word)visited.add(word) return uniq def get_unique_words_from_list_of_strings(str_list): return get_unique_words(' '.join(str_list)) words_in_order = get_unique_words_from_list_of_strings(text) print(words_in_order) 澄清一下:首先,您想将get_unique_words
应用于l
中的每个列表,然后要计数?
countInListOfLists
需要为words_in_order
中的每个单词应用到text
,而无需我每次都输入 print(countInListOfLists(text, ""))
我没有使用你的功能,但你可以这样做:uniqueText = set((" ".join(text)).split(" ")) print([countInListOfLists(text, word) for word in uniqueText])
。 (set
在设计上不包含重复项)【参考方案2】:
你可以作为一个班轮做到这一点 -
wordCount = len([word for word in (' ').join(text).split(' ') if word == 'duran'])
【讨论】:
【参考方案3】:你可以用下面的代码来完成它。
# check how many times a term has occurred in a list and its frequency
def count_occurrences(lst, term):
return lst.count(term)
# check the frequency of term in a list
def frequency(lst, term):
return count_occurrences(lst, term) / len(lst)
此外,我假设您会提前拆分列表元素以创建单词列表而不是句子列表,这会增加复杂性。否则,请参阅@CLRW97 回复。
将您的列表分成单词:
# split list of sentences into list of words
def split_sentences(lst):
return ' '.join(lst).split()
因此,您将拥有三个干净的功能供您使用:
# split list of sentences into list of words
def split_sentences(lst):
return ' '.join(lst).split()
# check how many times a term has occurred in a list and its frequency
def count_occurrences(lst, term):
lst = split_sentences(lst)
return lst.count(term)
# check the frequency of term in a list
def frequency(lst, term):
lst = split_sentences(lst)
return count_occurrences(lst, term) / len(lst)
text=["duran duran sang wild boys in 1984","wild boys don't
remain forever wild","who brought wild flowers","it was john krakauer who wrote in to the wild"]
print(count_occurrences(text, "duran"))
print(frequency(text, "duran"))
> 2
> 0.07407407407407407
但是,要获得更直接的答案,请查看 @DannyMoshe 是响应,它似乎也相当聪明。我的只是简单地解构为几个函数,以使代码更易于理解,同时还通过利用内置/简单的 Python 函数降低其复杂性。
【讨论】:
以上是关于如何检查一个术语在列表中出现的次数及其频率?的主要内容,如果未能解决你的问题,请参考以下文章