统计数集中出现最多的N个数(topK)

/******************************************************************
 * file:topk.c
 * brief:使用heap和hash_table统计超大数据量的数据中出现最多的N个数据
 * yejing@2015.3.1    1.0      creat
 ******************************************************************/  
  
 #include <stdio.h>
 #include <stdlib.h>
 #inlcude <string.h>
 #include <assert.h>
 
 #define parent(i)           (i)/2
 #define left_child(i)       ((i) << 1)
 #define right_child(i)      ((i) << 1) + 1
 
 
 #define HASH_TBL_SIZE       4194304
 #define HEAP_SIZE           32
 
 typedef hash_key_t   int
 typedef heap_data_t  int
 
 typedef struct _hash_node_t{
	hash_key_t key;
	int num;
	struct hash_node_t* next;
 }hash_node_t;
 static hash_node_t* hash_tbl[HASH_TBL_SIZE];
 
 typedef struct _min_heap_t{
	heap_data_t    data;
	int            num;
 }min_heap_t, heap[HEAP_SIZE + 1]
 
 int hash(hash_key_t key){
	return HASH_TBL_SIZEW%key;
 }
 
 hash_node_t* alloc_a_node(hash_key_t key){
	hash_node_t* node = (hash_node_t *)malloc(sizeof(node));
	if(!node)
		return NULL;
		
	node->key  = key;
	node->next = NULL;
	node->num  = 1;
	
	return node;
 }
 
 void node_insert(hash_key_t key){
	int tmp = hash(key);
	hash_node_t* node = hash_tbl[tmp];
	
	while(node){
		if(node->key == key){
			pnode->num++;
			return;
		}
		node = node->next;
	}
	
	if(!(node = creat_node(key)))
		assert(0);
	node->next = hash_tbl[tmp];
	hash_tbl[tmp] = node;
	return;
 }
 
 void heap_min_heaprify(min_heap_t min_heap, int size, int cursor){
	int left  = left_child(i);
	int right = right_child(i);
	int miniman;
	
	if(left < size && min_heap[left] < min_heap[cursor])
		miniman = left;
	else
		miniman = cusor;
	
	if(right < size && min_heap[right] < min_heap[minimam])
		miniman = right;
		
	if(miniman != cursor){
		swap(&min_heap[miniman], &min_heap[cursor]);
		heap_min_heaprify(min_heap, size, miniman);
	}
	
	return;
 }
 
 void build_min_heap(min_heap_t min_heap, int size){
	int i = 0;
	
	for(i = size/2; i >= 1; --i){
		heap_min_heaprify(min_heap, size, i);
	}
	
	return;
 }
 
 void topk_main(){
	hash_node_t* tmp = NULL;
	int i = 0;
	
	for(i = 0; i < HASH_TBL_SIZE; ++i){
		tmp = hash_table[i];
		if(tmp > min_heap[i].num){
			heap[1].num  = tmp->num;
			heap[1].data = tmp->key;
			heap_min_heaprify(heap, HEAP_SIZE, 1);
		}
		tmp = tmp->next;
	}
	
	return;
 }

### 回答1: 可以使用Python中的字典来统计文件中出现次数最多。 具体步骤如下: 1. 打开文件,读取文件中的所有字,将其存储在一个列表中。 2. 遍历列表,使用字典来统计个数出现次数。 3. 找到字典中出现次数最多字,即为文件中出现次数最多。 下面是具体的代码实现: ```python # 打开文件,读取所有字 with open('file.txt', 'r') as f: nums = [int(num) for num in f.read().split()] # 统计个数出现次数 count_dict = {} for num in nums: if num in count_dict: count_dict[num] += 1 else: count_dict[num] = 1 # 找到出现次数最多字 max_num = max(count_dict, key=count_dict.get) print('文件中出现次数最多是:', max_num) ``` 其中,`file.txt`是要统计的文件名,可以根据实际情况进行修改。 ### 回答2: 要统计一个文件中出现次数最多,我们可以使用 Python 的字典据类型。具体步骤如下: 1. 打开文件 使用 open() 函打开需要统计的文件,同时指定打开模式为只读模式 'r'。 ```python file = open('filename.txt', 'r') ``` 2. 读取文件内容 使用 readlines() 函读取文件内容,并将每一行据存储到一个列表中。 ```python lines = file.readlines() ``` 3. 统计出现次数 定义一个空的字典来存储每个数出现次数,并使用 for 循环遍历每一行据,将每个数以及它出现次数存储到字典中。 ```python count_dict = {} for line in lines: nums = line.strip().split(',') for num in nums: if num in count_dict: count_dict[num] += 1 else: count_dict[num] = 1 ``` 4. 找出出现次数最多 使用 max() 函找出字典中值最大的那个键值对,并返回它的键(也就是出现次数最多)。 ```python most_common_num = max(count_dict, key=count_dict.get) ``` 5. 输出结果 将结果打印出来,即为文件中出现次数最多。 ```python print(most_common_num) ``` 完整代码: ```python file = open('filename.txt', 'r') lines = file.readlines() count_dict = {} for line in lines: nums = line.strip().split(',') for num in nums: if num in count_dict: count_dict[num] += 1 else: count_dict[num] = 1 most_common_num = max(count_dict, key=count_dict.get) print(most_common_num) ``` 当然,以上代码还存在一些细节需要考虑,比如文件不存在或者格式不正确的情况,这需要根据具体情况进行判断和处理。 ### 回答3: Python中有很多方法可以统计一个文件中出现次数最多,这里提供两种方法。 方法一:使用字典 使用字典来统计文件中每个数出现次数,同时在遍历过程中记录最大出现次数和对应的。具体步骤如下: 1.打开文件并将其读入一个字符串变量中。 2.将字符串中的所有字用空格分开,并转换成整型,存入列表中。 3.遍历列表,用字典统计个数出现次数。 4.遍历字典,获取出现次数最多字。 代码如下: ```python with open('file.txt', 'r') as f: content = f.read() num_list = list(map(int, content.split())) num_count = {} max_count = 0 max_num = 0 for num in num_list: if num in num_count: num_count[num] += 1 else: num_count[num] = 1 if num_count[num] > max_count: max_count = num_count[num] max_num = num print("最多出现次数是:", max_num, "出现次数为:", max_count) ``` 方法二:使用Counter类 Python中有一个内置的Counter类,可以用来统计列表中各元素出现次数,可以极大地简化代码。具体步骤如下: 1.打开文件并将其读入一个字符串变量中。 2.将字符串中的所有字用空格分开,并转换成整型,存入列表中。 3.使用Counter类统计列表中各元素出现次数。 4.找到最常出现。 代码如下: ```python from collections import Counter with open('file.txt', 'r') as f: content = f.read() num_list = list(map(int, content.split())) num_count = Counter(num_list) max_num, max_count = num_count.most_common(1)[0] print("最多出现次数是:", max_num, "出现次数为:", max_count) ``` 总体来说,方法二比方法一更简洁易懂,建议使用Counter类来完成这个统计任务。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值