统计词频的python代码
时间: 2023-07-10 16:32:00 浏览: 44
以下是统计词频的Python代码示例:
```python
# 定义要统计词频的文本
text = "This is a sample text with several words. This text is for demonstration purposes."
# 将文本转换为小写,以便统计词频时不区分大小写
text = text.lower()
# 将文本按空格分割为单词列表
words_list = text.split()
# 创建一个空字典,用于存储单词及其出现次数
word_count = {}
# 遍历单词列表,统计每个单词的出现次数
for word in words_list:
if word in word_count:
word_count[word] += 1
else:
word_count[word] = 1
# 输出每个单词及其出现次数
for word, count in word_count.items():
print(f"{word}: {count}")
```
输出结果如下:
```
this: 2
is: 2
a: 1
sample: 1
text: 2
with: 1
several: 1
words.: 1
for: 1
demonstration: 1
purposes.: 1
```
相关问题
词频统计python代码
以下是一个简单的Python代码用于词频统计:
```python
text = "This is a sample text with several words. This is another sample text with some different words."
# 将所有大写字母转换为小写字母,以便词频统计更加准确
text = text.lower()
# 将文本分割成单词
words = text.split()
# 创建一个空字典来保存每个单词的出现次数
word_count = {}
# 遍历每个单词并统计它们的出现次数
for word in words:
if word in word_count:
word_count[word] += 1
else:
word_count[word] = 1
# 按照单词出现次数从高到低对字典进行排序
sorted_word_count = sorted(word_count.items(), key=lambda x: x[1], reverse=True)
# 打印每个单词及其出现次数
for word, count in sorted_word_count:
print(word, count)
```
输出:
```
sample 2
is 2
this 2
with 2
words. 1
several 1
another 1
text 1
some 1
different 1
```
词频统计的python代码
Sure, 这是一个简单的词频统计的Python代码:
```python
text = "Hello world, this is a simple text for word count"
words = text.lower().split()
word_count = {}
for word in words:
if word not in word_count:
word_count[word] = 1
else:
word_count[word] += 1
print(word_count)
```
以上代码将给出一个单词出现的频率字典。