list不能做词频统计吗
时间: 2023-08-11 17:06:25 浏览: 56
词频统计工具
list是可以做词频统计的,但需要先将文本分词并存储到list中,然后再通过遍历list中的词语,统计每个词语出现的次数。下面是一个简单的例子:
```python
text = "This is a sample text for word frequency counting. This text can be used to demonstrate how to count word frequency using list."
# 将文本分词并存储到list中
word_list = text.split()
# 统计每个词语出现的次数
word_count = {}
for word in word_list:
if word not 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: 1
a: 1
sample: 1
text: 2
for: 1
word: 2
frequency: 2
counting.: 1
can: 1
be: 1
used: 1
to: 1
demonstrate: 1
how: 1
count: 1
using: 1
list.: 1
```
阅读全文