import numpy import datetime def new_num(max_num, random_num): num = {} d = [i for i in range(1, 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] = 0 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 def get_num(num, choice, count): choice_list = [c[0] for c in choice[0:count]] red_list = [num[c - 1][0] for c in choice_list] return sorted(red_list) def print_num(color, num): if color == 'red': print('红球') elif color == 'blue': print('篮球') else: print('啥也不是!') print(', '.join([str(i) for i in num])) def print_time(): print('时间:' + datetime.datetime.strftime(datetime.datetime.now(), '%Y-%m-%d %H:%M:%S')) if __name__ == "__main__": print_time() print_num('red', get_num(new_num(33, 6), new_num(33, 6), 6)) print_num('blue', get_num(new_num(16, 1), new_num(16, 1), 1))