实现对划分的单词计数。
时间: 2023-12-03 08:44:33 浏览: 60
可以通过使用Python中的Counter类来实现对划分的单词计数。Counter类可以对一个可迭代对象中的元素进行计数,并返回一个字典,其中包含每个元素出现的次数。下面是一个示例代码:
```python
from collections import Counter
# 划分的单词列表
words = ['apple', 'banana', 'orange', 'apple', 'banana', 'pear', 'orange']
# 对单词进行计数
word_counts = Counter(words)
# 输出每个单词出现的次数
for word, count in word_counts.items():
print(f"{word}: {count}")
```
输出结果为:
```
apple: 2
banana: 2
orange: 2
pear: 1
```
在上面的代码中,我们首先导入了Python中的Counter类。然后,我们定义了一个包含多个单词的列表。接着,我们使用Counter类对这些单词进行计数,并将结果保存在一个名为`word_counts`的字典中。最后,我们使用`items()`方法遍历字典中的每个键值对,并输出每个单词出现的次数。
相关问题
代码设计,hadoop的单词计数功能
Hadoop的单词计数功能可以通过MapReduce编程模型来实现。具体步骤如下:
1. Mapper阶段:将输入文件划分成若干个小块,每个小块由一个Mapper进行处理。Mapper首先将输入的文本进行切分,将每个单词作为一个key,将其出现的次数作为value进行输出。
2. Shuffle阶段:将Mapper阶段输出的键值对按照key进行分组,将相同key的value合并在一起,得到一个key-value列表。
3. Reducer阶段:将Shuffle阶段输出的key-value列表按照key进行分组,然后对每个key的value进行汇总,得到最终的统计结果。
下面是一个简单的Hadoop单词计数的MapReduce程序实现:
Mapper代码:
```
public class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
private final static IntWritable one = new IntWritable(1);
private Text word = new Text();
public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String line = value.toString();
StringTokenizer tokenizer = new StringTokenizer(line);
while (tokenizer.hasMoreTokens()) {
word.set(tokenizer.nextToken());
context.write(word, one);
}
}
}
```
Reducer代码:
```
public class WordCountReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
private IntWritable result = new IntWritable();
public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
int sum = 0;
for (IntWritable val : values) {
sum += val.get();
}
result.set(sum);
context.write(key, result);
}
}
```
在main函数中进行Job的配置和提交:
```
Configuration conf = new Configuration();
Job job = Job.getInstance(conf, "word count");
job.setJarByClass(WordCount.class);
job.setMapperClass(WordCountMapper.class);
job.setCombinerClass(WordCountReducer.class);
job.setReducerClass(WordCountReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
```
以上代码实现了一个简单的Hadoop单词计数程序。
python统计文本中不同单词个数
可以用Python的字典来实现单词计数的功能,具体代码如下:
```python
text = "This is a sample text with several words and repeated words"
words = text.split() # 将文本划分成单词
word_count = {} # 创建一个空字典
# 遍历单词列表
for word in words:
if word not in word_count:
word_count[word] = 1 # 如果单词不在字典中,则将其加入字典,并将其计数设置为1
else:
word_count[word] += 1 # 如果单词已经在字典中,则将其计数加1
# 输出不同单词的个数
print("不同单词的个数:", len(word_count))
```
输出结果如下:
```
不同单词的个数: 9
```
这个代码可以统计文本中不同单词的个数,但是它并没有考虑到单词的大小写、标点符号等问题。如果需要更加精确的统计,还需要进行一些数据清洗的工作。