# 第一个代码块 import random # 导入random模块 import csv # 导入csv模块 from collections import defaultdict # 导入defaultdict模块 from colorama import Fore, Style # 导入Fore和Style模块 from itertools import zip_longest # 导入zip_longest模块 # 读取数据 filename = r"D:\数据库\双色球.txt" # 文件路径 data = [] # 初始化数据列表 with open(filename, 'r') as file: # 打开文件 reader = csv.reader(file) # 读取文件 for row in reader: # 遍历文件 red_balls = [int(num) for num in row[1].split(",")] # 将红球字符串转换为整数列表 blue_ball = int(row[2]) # 将篮球字符串转换为整数 data.append({"period": int(row[0]), "red_balls": red_balls, "blue_ball": blue_ball}) # 将期数、红球和篮球添加到数据列表中 # 初始化遗漏次数和出现次数 omission_counts = defaultdict(int) # 初始化遗漏次数 appear_counts = defaultdict(int) # 初始化出现次数 # 遍历数据,统计遗漏和出现次数,以及连续相同号码出现的次数 continuous_same_number_counts = defaultdict(int) # 初始化连续相同号码出现的次数 for i, d in enumerate(data): # 遍历数据 red_balls = d["red_balls"] # 获取红球列表 for red_ball in red_balls: # 遍历红球列表 if appear_counts[red_ball] == 0: # 如果红球没有出现过 omission_counts[red_ball] = i # 将遗漏次数设置为当前期数 else: # 如果红球出现过 continuous_same_number_counts[red_ball] += 1 # 将连续相同号码出现的次数加1 appear_counts[red_ball] += 1 # 将出现次数加1 # 统计篮球遗漏次数和出现次数 blue_omission_counts = defaultdict(int) # 初始化篮球遗漏次数 blue_appear_counts = defaultdict(int) # 初始化篮球出现次数 # 遍历数据,统计篮球遗漏和出现次数,以及连续相同号码出现的次数 blue_continuous_same_number_counts = defaultdict(int) # 初始化篮球连续相同号码出现的次数 for i, d in enumerate(data): # 遍历数据 blue_ball = d["blue_ball"] # 获取篮球 if blue_appear_counts[blue_ball] == 0: # 如果篮球没有出现过 blue_omission_counts[blue_ball] = i # 将篮球遗漏次数设置为当前期数 else: # 如果篮球出现过 blue_continuous_same_number_counts[blue_ball] += 1 # 将篮球连续相同号码出现的次数加1 blue_appear_counts[blue_ball] += 1 # 将篮球出现次数加1 # 统计命中率并输出推荐号码 print("全部红球命中率推荐:\t\t\t\t\t\t\t\t\t\t\t全部篮球命中率推荐:") # 输出标题 for i in range(17): # 遍历红球号码 red_ball = i+1 # 获取红球号码 hit_rate_red = appear_counts[red_ball] / len(data) * 100 # 计算红球命中率 if hit_rate_red >= 19: # 如果命中率大于等于19% color_red = Fore.RED # 设置红色字体 elif hit_rate_red >= 18.5: # 如果命中率大于等于18.5% color_red = Fore.YELLOW # 设置黄色字体 elif hit_rate_red >= 18: # 如果命中率大于等于18% color_red = Fore.MAGENTA # 设置洋红色字体 elif hit_rate_red >= 17.5: # 如果命中率大于等于17.5% color_red = Fore.GREEN # 设置绿色字体 elif hit_rate_red >= 17: # 如果命中率大于等于17% color_red = Fore.CYAN # 设置青色字体 elif hit_rate_red >= 16.5: # 如果命中率大于等于16.5% color_red = Fore.BLUE # 设置蓝色字体 elif hit_rate_red >= 16: # 如果命中率大于等于16% color_red = Fore.LIGHTMAGENTA_EX # 设置浅洋红色字体 else: # 如果命中率小于16% color_red = Fore.WHITE # 设置白色字体 if color_red == Fore.WHITE: # 如果字体颜色为白色 print(f"号码:{red_ball:02d},命中率:{hit_rate_red:.2f}%\t\t", end="") # 输出红球号码和命中率 else: # 如果字体颜色不为白色 print(f"{color_red}号码:{red_ball:02d},命中率:{hit_rate_red:.2f}%{Style.RESET_ALL}\t\t", end="") # 输出红球号码和命中率 if i + 17 < 34: # 如果还有另外一组红球号码 red_ball = i+16+1 # 获取另外一组红球号码 hit_rate_red = appear_counts[red_ball] / len(data) * 100 # 计算红球命中率 if hit_rate_red >= 19: # 如果命中率大于等于19% color_red = Fore.RED # 设置红色字体 elif hit_rate_red >= 18.5: # 如果命中率大于等于18.5% color_red = Fore.YELLOW # 设置黄色字体 elif hit_rate_red >= 18: # 如果命中率大于等于18% color_red = Fore.MAGENTA # 设置洋红色字体 elif hit_rate_red >= 17.5: # 如果命中率大于等于17.5% color_red = Fore.GREEN # 设置绿色字体 elif hit_rate_red >= 17: # 如果命中率大于等于17% color_red = Fore.CYAN # 设置青色字体 elif hit_rate_red >= 16.5: # 如果命中率大于等于16.5% color_red = Fore.BLUE # 设置蓝色字体 elif hit_rate_red >= 16: # 如果命中率大于等于16% color_red = Fore.LIGHTMAGENTA_EX # 设置浅洋红色字体 else: # 如果命中率小于16% color_red = Fore.WHITE # 设置白色字体 if color_red == Fore.WHITE: # 如果字体颜色为白色 print(f"号码:{red_ball:02d},命中率:{hit_rate_red:.2f}%\t\t", end="") # 输出红球号码和命中率 else: # 如果字体颜色不为白色 print(f"{color_red}号码:{red_ball:02d},命中率:{hit_rate_red:.2f}%{Style.RESET_ALL}\t\t", end="") # 输出红球号码和命中率 blue_ball = i+1 # 获取篮球号码 hit_rate_blue = blue_appear_counts[blue_ball] / len(data) * 100 # 计算篮球命中率 if hit_rate_blue >= 6.4: # 如果命中率大于等于6.4% color_blue = Fore.RED # 设置红色字体 elif hit_rate_blue >= 6.2: # 如果命中率大于等于6.2% color_blue = Fore.YELLOW # 设置黄色字体 elif hit_rate_blue >= 6: # 如果命中率大于等于6% color_blue = Fore.MAGENTA # 设置洋红色字体 elif hit_rate_blue >= 5.8: # 如果命中率大于等于5.8% color_blue = Fore.GREEN # 设置绿色字体 elif hit_rate_blue >= 5.6: # 如果命中率大于等于5.6% color_blue = Fore.CYAN # 设置青色字体 else: # 如果命中率小于5.6% color_blue = Fore.WHITE # 设置白色字体 if color_blue == Fore.WHITE: # 如果字体颜色为白色 print(f"篮球:{blue_ball:02d},命中率:{hit_rate_blue:.2f}%") # 输出篮球号码和命中率 else: # 如果字体颜色不为白色 print(f"篮球:{blue_ball:02d},命中率:{color_blue}{hit_rate_blue:.2f}%{Fore.RESET}") # 输出篮球号码和命中率
数据格式:
2023001,"09,16,18,22,28,32",02
2023002,"02,06,10,16,18,22",13
2023003,"05,08,18,25,30,32",06
2023004,"01,07,15,16,20,25",16
2023005,"02,06,08,13,14,19",16
2023006,"02,10,12,14,24,26",05
2023007,"05,06,09,22,25,31",03
2023008,"05,13,18,20,26,28",06
2023009,"03,04,19,23,30,32",06
2023010,"04,05,13,18,27,29",12
2023011,"06,07,16,17,21,22",04
2023012,"03,04,07,22,26,30",06
2023013,"06,10,14,22,23,33",13
2023014,"06,07,18,23,27,30",05
2023015,"02,03,14,21,29,32",08
2023016,"14,16,19,23,28,30",03
2023017,"05,11,18,20,26,28",12
2023018,"10,12,17,19,25,31",13
2023019,"06,12,26,28,29,32",15
2023020,"01,12,17,18,26,27",05
2023021,"02,05,06,19,21,27",04
2023022,"10,11,18,19,23,31",03
2023023,"05,08,10,15,24,25",09
需要空一行