python使用set完成词频统计
时间: 2023-10-16 11:08:28 浏览: 118
假设有一个字符串s,要统计其中每个单词(以空格为分隔符)出现的次数,可以使用set来辅助实现。
具体步骤如下:
1. 将字符串s拆分为单词列表words,可以使用split()函数实现。
2. 创建一个空的字典freq_dict,用于存储每个单词的出现次数。
3. 遍历单词列表words,对于每个单词,判断它是否已经在freq_dict中存在,如果存在,则将对应的计数器加1;如果不存在,则将这个单词加入freq_dict中,并将对应的计数器初始化为1。
4. 遍历完成后,freq_dict中存储的就是每个单词出现的次数了。
代码实现如下:
```
s = "this is a test string for testing string functions"
# 将字符串拆分为单词列表
words = s.split()
# 创建空字典,用于存储单词出现次数
freq_dict = {}
# 遍历单词列表,统计每个单词出现的次数
for word in words:
if word in freq_dict:
freq_dict[word] += 1
else:
freq_dict[word] = 1
# 输出结果
for word, freq in freq_dict.items():
print(word, freq)
```
输出结果如下:
```
this 1
is 2
a 1
test 2
string 2
for 1
testing 1
functions 1
```
相关问题
使用set实现词频统计
可以使用Python中的set和字典来实现词频统计。
1. 先将文本分词,并去除停用词(如“的”、“了”、“在”等无实际意义的词)。
2. 使用set来去重,得到所有出现过的单词。
3. 创建一个空的字典,用于存储每个单词出现的次数。
4. 遍历分词后的文本,使用字典统计每个单词出现的次数。
5. 最后,将字典按照词频从高到低排序,即可得到词频统计结果。
代码示例:
```python
import jieba
import re
# 读取文本并进行分词
with open('text.txt', 'r', encoding='utf-8') as f:
text = f.read()
word_list = jieba.cut(text)
stopwords = ['的', '了', '在', '是'] # 停用词表
word_list = [word for word in word_list if word not in stopwords]
# 统计词频
word_set = set(word_list) # 去重
word_dict = {}
for word in word_list:
if word not in word_dict:
word_dict[word] = 1
else:
word_dict[word] += 1
# 按照词频排序
word_freq = sorted(word_dict.items(), key=lambda x: x[1], reverse=True)
# 输出结果
for word, freq in word_freq:
print(word, freq)
```
注意:由于jieba分词工具需要安装,因此需要先使用pip安装jieba库。
python共线词频统计
您好!要在Python中执行共线词频统计,您可以按照以下步骤进行操作:
1. 导入所需的库:
```python
import nltk
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
from nltk.probability import FreqDist
```
2. 下载停用词数据(如果尚未下载过):
```python
nltk.download('stopwords')
```
3. 加载文本数据并进行预处理:
```python
text = "在这里输入您的文本数据"
text = text.lower() # 将文本转换为小写字母
```
4. 分词和过滤停用词:
```python
stop_words = set(stopwords.words('english')) # 根据需要选择适合的语言
tokens = word_tokenize(text) # 将文本分词为单词
filtered_tokens = [word for word in tokens if word.isalpha() and word not in stop_words] # 过滤掉非字母字符和停用词
```
5. 计算词频:
```python
freq_dist = FreqDist(filtered_tokens) # 计算词频分布
```
6. 打印最常见的词及其频率:
```python
for word, freq in freq_dist.most_common():
print(word, freq)
```
以上是一个基本的共线词频统计的示例。您可以根据实际需求对代码进行修改和扩展。希望对您有所帮助!如有任何问题,请随时提问。
阅读全文
相关推荐
















