python双色球数据统计
时间: 2023-10-26 08:08:23 浏览: 162
Python的Panda库在数据处理方面非常强大,可以非常出色地完成双色球的数据统计。你可以使用Counter函数从文件中读取数据,并对红球和蓝球进行出现次数统计。最后,你可以使用most_common函数找出出现频率最高的六个红球和蓝球。下面是一个示例代码:
```python
# 导入Counter
from collections import Counter
def readfile():
red_lists = [] # 存放红球的列表
blue_lists = [] # 存放蓝球的列表
# 打开文件并获取文件句柄
with open("./balls.txt", "r", encoding='utf-8') as fp:
# 开始读取文件并返回一个list
list1 = fp.readlines()
# 遍历整个文件内容
for i in range(len(list1)):
# 替换掉\n的字符再按空格分隔
list2 = str(list1[i]).replace("\n", "").split(" ")
for j in range(len(list2)):
if j == 6:
# 蓝球放入到blue_lists 列表中
blue_lists.append(list2[j])
else:
# 红球放入到red_lists 列表中
red_lists.append(list2[j])
# Counter可以快速便捷地对某些对象做一些统计操作,这里是对列表里面的数据进行出现次数统计,返回一个tuple
red_count = Counter(red_lists)
blue_count = Counter(blue_lists)
# most_common可以用来统计列表或字符串中最常出现的元素并做排序,并返回一个list
k = red_count.most_common(len(red_count))
# 输出出现频率最高的六个红球
print("the red ball:", k[:6])
l = blue_count.most_common(len(blue_count))
# 输出出现频率最高的六个蓝球
print("the blue ball:", l[:6])
if __name__ == "__main__":
readfile()
```
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.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://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)