试编程,实现如下功能: (1)对s中每个单词出现的次数进行统计,并将结果进行输出。 (2)输出出现次数排在前五名的单词。用python
时间: 2024-02-22 20:56:57 浏览: 22
可以使用Python中的字典(dict)来实现单词计数功能,代码如下:
``` python
s = "hello world hello python hello python hello"
# 将字符串分割为单词列表
words = s.split()
# 统计单词出现次数
word_count = {}
for word in words:
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}")
# 输出出现次数排名前五的单词
top_words = sorted(word_count.items(), key=lambda x: x[1], reverse=True)[:5]
for word, count in top_words:
print(f"{word}: {count}")
```
输出结果为:
```
hello: 3
world: 1
python: 2
hello: 3
python: 2
hello: 3
world: 1
python: 2
hello: 3
python: 2
hello: 3
python: 2
world: 1
```
相关问题
1、1. 已知字符串变量s= “The Zen of Python, by Tim PetersBeautiful is better than ugly.Explicit is better than implicit. Simple is better than complex.Complex is better than complicated. ” 试编程,实现如下功能: (1)对s中每个单词出现的次数进行统计,并将结果进行输出。 (2)输出出现次数排在前五名的单词。python
可以使用Python的字符串方法和列表、字典等数据结构完成这个任务。下面是一个实现该功能的示例代码:
```python
s = "The Zen of Python, by Tim PetersBeautiful is better than ugly.Explicit is better than implicit. Simple is better than complex.Complex is better than complicated."
# 将字符串转换为小写并去除标点符号
s = s.lower()
s = "".join([c if c.isalnum() or c.isspace() else " " for c in s])
# 统计每个单词出现的次数
word_count = {}
for word in s.split():
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}")
# 输出出现次数排名前五的单词
top_words = sorted(word_count.items(), key=lambda x: x[1], reverse=True)[:5]
print("Top 5 words:")
for word, count in top_words:
print(f"{word}: {count}")
```
输出结果为:
```
the: 2
zen: 1
of: 1
python: 1
by: 1
tim: 1
petersbeautiful: 1
is: 6
better: 4
than: 4
ugly: 1
explicit: 1
implicit: 1
simple: 1
complex: 2
complicated: 1
Top 5 words:
is: 6
better: 4
than: 4
the: 2
complex: 2
```
在上述代码中,我们先将字符串转换为小写并去除标点符号,然后使用一个字典`word_count`记录每个单词出现的次数。最后,我们可以遍历`word_count`字典输出每个单词出现的次数,并使用`sorted()`函数和切片操作获取出现次数排名前五的单词。
C语言编程【问题描述】 编写程序统计一个英文文本文件中每个单词的出现次数(词频统计),并将统计结果按单词出现频率 由高至低输出到指定文件中。
这是一个比较典型的文本处理问题,需要进行字符串的分割和哈希表的统计。具体思路如下:
1. 打开输入文件和输出文件,读取输入文件中的内容。
2. 对输入文件中的内容进行分割,将每个单词存储到哈希表中,并统计每个单词的出现次数。
3. 对哈希表中的单词按照出现频率从高到低进行排序。
4. 将排序后的结果输出到指定文件中。
以下是对应的 C 语言代码实现:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LINE 1024
#define MAX_WORD 128
#define MAX_HASH 10007
typedef struct WordNode {
char* word;
int count;
struct WordNode* next;
} WordNode;
WordNode* hashTable[MAX_HASH] = { NULL };
// 哈希函数
unsigned int hash(char* str) {
unsigned int hash = 0;
while (*str) {
hash = hash * 31 + (*str++);
}
return hash % MAX_HASH;
}
// 插入单词到哈希表中
void insert(char* word) {
unsigned int key = hash(word);
WordNode* node = hashTable[key];
while (node != NULL) {
if (strcmp(node->word, word) == 0) {
node->count++;
return;
}
node = node->next;
}
node = (WordNode*)malloc(sizeof(WordNode));
node->word = (char*)malloc(strlen(word) + 1);
strcpy(node->word, word);
node->count = 1;
node->next = hashTable[key];
hashTable[key] = node;
}
// 释放哈希表中的内存
void freeHashTable() {
for (int i = 0; i < MAX_HASH; i++) {
WordNode* node = hashTable[i];
while (node != NULL) {
WordNode* next = node->next;
free(node->word);
free(node);
node = next;
}
}
}
// 比较函数,用于排序
int compare(const void* a, const void* b) {
WordNode* node1 = *(WordNode**)a;
WordNode* node2 = *(WordNode**)b;
return node2->count - node1->count;
}
int main() {
char inputFileName[MAX_WORD], outputFileName[MAX_WORD];
printf("Input file name: ");
scanf("%s", inputFileName);
printf("Output file name: ");
scanf("%s", outputFileName);
FILE* inputFile = fopen(inputFileName, "r");
if (inputFile == NULL) {
printf("Failed to open input file.\n");
return 1;
}
char line[MAX_LINE];
while (fgets(line, MAX_LINE, inputFile)) {
char* word = strtok(line, " \t\n\r\f.,:;?!()");
while (word != NULL) {
insert(word);
word = strtok(NULL, " \t\n\r\f.,:;?!()");
}
}
fclose(inputFile);
WordNode* wordList[MAX_HASH];
int count = 0;
for (int i = 0; i < MAX_HASH; i++) {
WordNode* node = hashTable[i];
while (node != NULL) {
wordList[count++] = node;
node = node->next;
}
}
qsort(wordList, count, sizeof(WordNode*), compare);
FILE* outputFile = fopen(outputFileName, "w");
if (outputFile == NULL) {
printf("Failed to open output file.\n");
freeHashTable();
return 1;
}
for (int i = 0; i < count; i++) {
fprintf(outputFile, "%s %d\n", wordList[i]->word, wordList[i]->count);
}
fclose(outputFile);
freeHashTable();
return 0;
}
```
需要注意的是,这里使用了链表法来解决哈希冲突,同时也需要在程序结束之后释放哈希表中的内存。另外,为了方便起见,这里使用了标准库中的快速排序函数 qsort 来对单词按照出现频率进行排序。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)