📌  相关文章
📜  字符串列表中最常用单词的Python程序

📅  最后修改于: 2022-05-13 01:55:44.577000             🧑  作者: Mango

字符串列表中最常用单词的Python程序

给定字符串列表,编写一个Python程序来获取出现次数最多的单词。

例子:

方法 #1:使用循环 + max() + split() + defaultdict()

在这里,我们使用 split() 执行获取每个单词的任务,并通过使用 defaultdict() 记住它来增加它的频率。最后, max() 与参数一起使用以获取最大频率字符串的计数。

Python3
# Python3 code to demonstrate working of
# Most frequent word in Strings List
# Using loop + max() + split() + defaultdict()
from collections import defaultdict
 
# initializing Matrix
test_list = ["gfg is best for geeks", "geeks love gfg", "gfg is best"]
 
# printing original list
print("The original list is : " + str(test_list))
 
temp = defaultdict(int)
 
# memoizing count
for sub in test_list:
    for wrd in sub.split():
        temp[wrd] += 1
 
# getting max frequency
res = max(temp, key=temp.get)
 
# printing result
print("Word with maximum frequency : " + str(res))


Python3
# Python3 code to demonstrate working of
# Most frequent word in Strings List
# Using list comprehension + mode()
from statistics import mode
 
# initializing Matrix
test_list = ["gfg is best for geeks", "geeks love gfg", "gfg is best"]
 
# printing original list
print("The original list is : " + str(test_list))
 
# getting all words
temp = [wrd for sub in test_list for wrd in sub.split()]
 
# getting frequency
res = mode(temp)
 
# printing result
print("Word with maximum frequency : " + str(res))


Python3
# Python3 code to demonstrate working of
# Most frequent word in Strings List
 
from collections import Counter
 
# function which returns
# most frequent word
def mostFrequentWord(words):
   
    # Taking empty list
    lis = []
    for i in words:
       
        # Getting all words
        for j in i.split():
            lis.append(j)
             
    # Calculating frequency of all words
    freq = Counter(lis)
     
    # find max count and print that key
    max = 0
    for i in freq:
        if(freq[i] > max):
            max = freq[i]
            word = i
            return word
 
 
# Driver code
# initializing strings list
words = ["gfg is best for geeks", "geeks love gfg", "gfg is best"]
 
# printing original list
print("The original list is : " + str(words))
 
# passing this words to mostFrequencyWord function
# printing result
print("Word with maximum frequency : " + mostFrequentWord(words))
# This code is contributed by vikkycirus


输出:

The original list is : ['gfg is best for geeks', 'geeks love gfg', 'gfg is best']
Word with maximum frequency : gfg

方法 #2:使用列表理解 + mode()

在这里,我们使用列表理解获得所有单词,并使用 mode() 获得最大频率。

蟒蛇3

# Python3 code to demonstrate working of
# Most frequent word in Strings List
# Using list comprehension + mode()
from statistics import mode
 
# initializing Matrix
test_list = ["gfg is best for geeks", "geeks love gfg", "gfg is best"]
 
# printing original list
print("The original list is : " + str(test_list))
 
# getting all words
temp = [wrd for sub in test_list for wrd in sub.split()]
 
# getting frequency
res = mode(temp)
 
# printing result
print("Word with maximum frequency : " + str(res))

输出:

The original list is : ['gfg is best for geeks', 'geeks love gfg', 'gfg is best']
Word with maximum frequency : gfg

方法 #3:使用 list() 和 Counter()

  • 将所有单词附加到空列表中并使用Counter()函数计算所有单词的频率。
  • 查找最大计数并打印该键。

下面是实现:

蟒蛇3

# Python3 code to demonstrate working of
# Most frequent word in Strings List
 
from collections import Counter
 
# function which returns
# most frequent word
def mostFrequentWord(words):
   
    # Taking empty list
    lis = []
    for i in words:
       
        # Getting all words
        for j in i.split():
            lis.append(j)
             
    # Calculating frequency of all words
    freq = Counter(lis)
     
    # find max count and print that key
    max = 0
    for i in freq:
        if(freq[i] > max):
            max = freq[i]
            word = i
            return word
 
 
# Driver code
# initializing strings list
words = ["gfg is best for geeks", "geeks love gfg", "gfg is best"]
 
# printing original list
print("The original list is : " + str(words))
 
# passing this words to mostFrequencyWord function
# printing result
print("Word with maximum frequency : " + mostFrequentWord(words))
# This code is contributed by vikkycirus

输出:

The original list is : ['gfg is best for geeks', 'geeks love gfg', 'gfg is best']
Word with maximum frequency : gfg