import numpy # 字典排序 def dict_sort(_dict, _sort_by='key', _reverse=False): if _sort_by == 'key': _result = sorted(_dict.items(), key=lambda x: x[0], reverse=_reverse) else: _result = sorted(_dict.items(), key=lambda x: x[1], reverse=_reverse) _result_dict = dict() for r in _result: _result_dict[r[0]] = r[1] return _result_dict # 计算偏差值 def get_analysis_data(_raw_dict): total = 0 for _num in _raw_dict.values(): total += _num average = total / len(_raw_dict.keys()) _delta_dict = dict() for _key, _val in _raw_dict.items(): _delta_dict[_key] = round(_raw_dict[_key] - average, 2) return _delta_dict def print_data(_data_list): new_data_list = [list(d.items()) for d in _data_list] for i in range(len(new_data_list[0])): print_str = f"" for data in new_data_list: print_str += f"{data[i][0]} [{data[i][1]}]\t" print(print_str) def new_num(min_num, max_num, random_num): num = {} d = [i for i in range(min_num, max_num + 1)] n = 100000 while n > 0: result = numpy.random.choice(d, random_num, replace=False) # print(sorted(r)) for r in result: if r in num.keys(): num[r] += 1 else: num[r] = 1 n -= 1 sorted_result = sorted(num.items(), key=lambda x: x[0]) red_result_dict = {} for item in sorted_result: red_result_dict[item[0]] = item[1] # print(red_result_dict) sorted_result_by_value = sorted(num.items(), key=lambda x: x[1], reverse=True) # print(sorted_result_by_value) return sorted_result_by_value