Python 以查找在字符串列表中常见的元素的频率程序

Python 以查找在字符串列表中常见的元素的频率程序

在这篇Python文章中,给定的任务是获取在字符串列表中常见元素的频率。有时使用Python分析的列表在Excel文件中可用。为了从Excel获取此列表,使用了一个称为openpyxl的模块。在这篇Python文章中,使用三个不同的示例给出了获取在字符串列表中重复出现的项的频率的方法。在示例1中,找到了在字符串列表中常见的字符的频率。在接下来的两个示例中,给出了在字符串列表中常见的单词的频率的方法。在这些示例中,字符串列表是从Excel文件的列中获取的。

预处理步骤

步骤1 - 使用Google账号登录。转到Google Colab。打开一个新的Colab笔记本,在其中编写Python代码。

步骤2 - 首先将Excel文件”oldrecord5.xlsx”上传到Google Colab。

步骤3 - 导入“openpyxl”。

步骤4 - 使用openpyxl.load_workbook函数加载excel文件。

步骤5 - 将活动工作表打开并赋值给一个名为myxlsxsheet的变量。

步骤6 - 使用Pandas将此字符串列获取为数据帧。

步骤7 - 将数据帧转换为列表。将此列表称为“title_list”。

用于这些示例的Excel文件内容

Python 以查找在字符串列表中常见的元素的频率程序

图:显示在示例中使用的Excel文件oldrecord5.xls

将Excel文件上传到Colab

Python 以查找在字符串列表中常见的元素的频率程序

图:将oldrecord5.xls上传到Google Colab

示例1:使用reduce函数获取在字符串列表中找到的字符的频率

在这种方法中,使用了reduce函数。

步骤1 - 使用预处理步骤中的“title_list”列表。

步骤2 - 使用reduce、lambda和Counter来查找这些字符串中所有常见字符的频率。

步骤3 - 以字典的形式显示结果。

在Google Colab工作表的代码单元格中编写以下代码

import openpyxl
from openpyxl import load_workbook
import pandas as pd
from functools import reduce
from collections import Counter
# load excel file with its path
myxlsx = openpyxl.load_workbook("oldrecord5.xlsx")
myxlsxsheet = myxlsx.active

# Convert to DataFrame
df = pd.DataFrame(myxlsxsheet.values)

#Select those rows that contain "Discussion" String
df1=df[df.iloc[:,3].str.contains('Discussion')]

#Select only the titles' column 
df2 = df1.iloc[:,3]

title_list=df2.values.tolist()
print(title_list)

itemFreq = reduce(lambda m, n: m & n, (Counter(elem) for elem in title_list[1:]),Counter(title_list[0]))

print("Common Characters and their occurrence : " , str(dict(itemFreq)))

查看结果

点击代码单元上的播放按钮以查看结果。

['Types of Environment - Class Discussion', 'Management Structure and Nature  - Class Discussion', 'Macro- Demography, Natural, Legal & Political  - Class Discussion']
Common Characters and their occurrence :  {'e': 2, 's': 5, ' ': 5, 'o': 1, 'n': 1, 'i': 2, 'r': 1, 'm': 1, 't': 1, '-': 1, 'C': 1, 'l': 1, 'a': 1, 'D': 1, 'c': 1, 'u': 1}

第一张图:显示使用Google Colab的结果。

示例2:通过组合和排序列表,获取在一列字符串中找到的单词的频率

要遵循这种方法,我们使用以下步骤

步骤1 − 使用预处理步骤中的列表“title_list”。

步骤2 − 使用split将各个列表项分隔成单词,然后将这些单词组合成一个组合列表。

步骤3 − 对该组合列表进行排序,并使用Counter找到频率。以字典的形式显示结果。

将以下代码写入Google Colab工作表的代码单元格中。

from collections import Counter
import openpyxl
from openpyxl import load_workbook
import pandas as pd

# load excel file with its path
myxlsx = openpyxl.load_workbook("oldrecord5.xlsx")
myxlsxsheet = myxlsx.active

# Convert to DataFrame
df = pd.DataFrame(myxlsxsheet.values)

#Select those rows that contain "Discussion" String
df1=df[df.iloc[:,3].str.contains('Discussion')]

#Select only titles' column 
df2 = df1.iloc[:,3]

title_list=df2.values.tolist()
print(title_list)
lst1= title_list[0].split()
lst2= title_list[1].split()
lst3= title_list[2].split()
combinedlist = [*lst1, *lst2, *lst3]

# Print output
print("Concatenated List: ",combinedlist)

for elem in sorted(combinedlist):
   print(elem)

frequencyofelements=Counter(combinedlist)
print("frequency of elements: ",frequencyofelements)

示例2的输出结果

要在colab中查看结果,请点击代码单元格上的播放按钮。

['Types of Environment - Class Discussion', 'Management Structure and Nature  - Class Discussion', 'Macro- Demography, Natural, Legal & Political  - Class Discussion']
Concatenated List:  ['Types', 'of', 'Environment', '-', 'Class', 'Discussion', 'Management', 'Structure', 'and', 'Nature', '-', 'Class', 'Discussion', 'Macro-', 'Demography,', 'Natural,', 'Legal', '&', 'Political', '-', 'Class', 'Discussion']
&
-
-
-
Class
Class
Class
Demography,
Discussion
Discussion
Discussion
Environment
Legal
Macro-
Management
Natural,
Nature
Political
Structure
Types
and
of
frequency of elements:  Counter({'-': 3, 'Class': 3, 'Discussion': 3, 'Types': 1, 'of': 1, 'Environment': 1, 'Management': 1, 'Structure': 1, 'and': 1, 'Nature': 1, 'Macro-': 1, 'Demography,': 1, 'Natural,': 1, 'Legal': 1, '&': 1, 'Political': 1})

图2:使用Google Colab显示结果。

示例3:使用pandas和它们的功能获取在字符串列表中找到的单词的频率

要按照这种方法进行操作,我们使用了以下步骤

第一步 − 使用上面预处理步骤中的“title_list”列表。

第二步 − 对每个列表项使用split来将它们分隔成单词,然后将这些单词组合成一个合并的列表。

第三步 − 使用Pandas的Series,然后使用value_counts()函数来计算使用的单词的频率。展示输出。

在Google Colab工作表的代码单元中编写以下代码。

import openpyxl
from openpyxl import load_workbook
import pandas as pd

# load excel file with its path
myxlsx = openpyxl.load_workbook("oldrecord5.xlsx")
myxlsxsheet = myxlsx.active

# Convert to DataFrame
df = pd.DataFrame(myxlsxsheet.values)

#Select those rows that contain "Discussion" String
df1=df[df.iloc[:,3].str.contains('Discussion')]

#Select only titles' column 
df2 = df1.iloc[:,3]

title_list=df2.values.tolist()
print(title_list)
lst1= title_list[0].split()
lst2= title_list[1].split()
lst3= title_list[2].split()

#combinedlist = [*lst1, *lst2, *lst3, *lst4, *lst5]
combinedlist = [*lst1, *lst2, *lst3]
# Print output
print("Concatenated List: ",combinedlist)

frequencyofelements = pd.Series(combinedlist).value_counts()
print("frequency of elements: ") 
print(frequencyofelements)

查看结果

按下代码单元格上的播放按钮以查看结果

['Types of Environment - Class Discussion', 'Management Structure and Nature  - Class Discussion', 'Macro- Demography, Natural, Legal & Political  - Class Discussion']
Concatenated List:  ['Types', 'of', 'Environment', '-', 'Class', 'Discussion', 'Management', 'Structure', 'and', 'Nature', '-', 'Class', 'Discussion', 'Macro-', 'Demography,', 'Natural,', 'Legal', '&', 'Political', '-', 'Class', 'Discussion']
frequency of elements: 
-              3
Class          3
Discussion     3
Types          1
of             1
Environment    1
Management     1
Structure      1
and            1
Nature         1
Macro-         1
Demography,    1
Natural,       1
Legal          1
&              1
Political      1
dtype: int64

在这篇Python文章中,通过三个不同的示例,展示了如何找到在字符串列表中出现的元素的频率的方法。在第一个示例中,将元素视为在字符串中出现的简单字符来处理。在第二个和第三个示例中,首先将字符串分解为个别的有意义的单词,然后将它们作为元素来计算频率。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程