目錄
- 一、需求介紹
- 二、數(shù)據(jù)分析
- 2.1 獲取一天的數(shù)據(jù)
- 2.2 開(kāi)始一天的數(shù)據(jù)的分析
- 2.3 循環(huán)日期進(jìn)行多天的數(shù)據(jù)分析:
- 2.4 將數(shù)據(jù)寫(xiě)入Excel表格中
- 三、完整代碼
- 四、運(yùn)行結(jié)果
- 五、總結(jié)
一、需求介紹
該需求主要是分析彩票的歷史數(shù)據(jù)
客戶的需求是根據(jù)彩票的前兩期的情況,如果存在某個(gè)斜著的兩個(gè)數(shù)字相等,那么就買(mǎi)第三期的同一個(gè)位置處的彩票
對(duì)于1、,如果相等的數(shù)字是:1-5,那就買(mǎi)6-10,如果相等的數(shù)字是:6-10,那就買(mǎi)1-5;
對(duì)于2、,如果相等的數(shù)字是:1-5,那就買(mǎi)1-5,如果相等的數(shù)字是:6-10,,那就買(mǎi)6-10。
然后,根據(jù)這個(gè)方案,有可能會(huì)買(mǎi)中,但是也有可能買(mǎi)不中,于是,客戶希望我可以統(tǒng)計(jì)出來(lái)在100天中,按照這種方法,連續(xù)6次以及6次以上的購(gòu)買(mǎi)彩票才能夠命中一次獎(jiǎng)的次數(shù)以及分別所對(duì)應(yīng)的時(shí)間,對(duì)于這個(gè)案例,我們下面詳細(xì)分析。
二、數(shù)據(jù)分析
(在這里,我們先利用 Jupyter Notebook 來(lái)進(jìn)行分析,然后,在得到成果以后,利用 Pycharm 來(lái)進(jìn)行完整的程序設(shè)計(jì)。)
2.1 獲取一天的數(shù)據(jù)
打開(kāi)如下圖所示的界面可以獲取到網(wǎng)址以及請(qǐng)求頭:
網(wǎng)址(歷史數(shù)據(jù)的網(wǎng)址)
請(qǐng)求頭
然后我們?cè)诔绦蛑羞M(jìn)行代碼書(shū)寫(xiě)獲取數(shù)據(jù):
然后進(jìn)行一定的預(yù)處理:
2.2 開(kāi)始一天的數(shù)據(jù)的分析
這里我們直接展示代碼:
def reverse_list(lst):
"""
準(zhǔn)換列表的先后順序
:param lst: 原始列表
:return: 新的列表
"""
return [ele for ele in reversed(lst)]
low_list = ["01", "02", "03", "04", "05"]
# 設(shè)置比較小的數(shù)字的列表
high_list = ["06", "07", "08", "09", "10"]
# 設(shè)置比較大的數(shù)字的列表
N = 0
# 設(shè)置一個(gè)數(shù)字N來(lái)記錄一共有多少期可以購(gòu)買(mǎi)
n = 0
# 設(shè)置一個(gè)數(shù)字n來(lái)記錄命中了多少期彩票
record_number = 1
# 設(shè)置記錄數(shù)據(jù)的一個(gè)判斷值
list_data_number = []
# 設(shè)置一個(gè)空的列表來(lái)存儲(chǔ)一天之中的連續(xù)掛掉的期數(shù)
dict_time_record = {}
# 設(shè)置一個(gè)空的字典來(lái)存儲(chǔ)連掛掉的期數(shù)滿足所列條件的時(shí)間節(jié)點(diǎn)
for k in range(1152):
# 循環(huán)遍歷所有的數(shù)據(jù)點(diǎn)
if k 1150:
new_result1 = reverse_list(new_response["result"]["data"])[k]
# 第一期數(shù)據(jù)
new_result2 = reverse_list(new_response["result"]["data"])[k + 1]
# 第二期數(shù)據(jù)
new_result3 = reverse_list(new_response["result"]["data"])[k + 2]
# 第三期數(shù)據(jù)
data1 = new_result1['preDrawCode'].split(',')
# 第一期數(shù)據(jù)
data2 = new_result2['preDrawCode'].split(',')
# 第二期數(shù)據(jù)
data3 = new_result3['preDrawCode'].split(',')
# 第三期數(shù)據(jù)
for m in range(10):
# 通過(guò)循環(huán)來(lái)判斷是否滿足購(gòu)買(mǎi)的條件,并且實(shí)現(xiàn)一定的功能
if m == 0:
if data2[0] == data1[1]:
# 如果相等就要結(jié)束循環(huán)
N += 1
# 可以購(gòu)買(mǎi)的期數(shù)應(yīng)該要自加一
if (data2[0] in low_list and data3[0] in low_list) or (data2[0] in high_list and data3[0] in high_list):
n += 1
# 命中的期數(shù)應(yīng)該要自加一
# 如果命中了的話,本輪結(jié)束,開(kāi)啟下一輪
list_data_number.append(record_number)
if f"{record_number}" in dict_time_record.keys():
# 如果已經(jīng)有了這個(gè)鍵,那么值添加時(shí)間點(diǎn)
dict_time_record[f"{record_number}"].append(new_result3['preDrawTime'][11:])
else:
# 如果沒(méi)有這個(gè)鍵,那么添加一個(gè)鍵值對(duì),值為一個(gè)列表,而且初始化為當(dāng)前的時(shí)間
dict_time_record[f"{record_number}"] = [new_result3['preDrawTime'][11:]]
record_number = 1
# 初始化下一輪的開(kāi)始
else:
record_number += 1
# 如果沒(méi)有命中的話,次數(shù)就應(yīng)該要自加一
break
# 如果滿足相等的條件就要結(jié)束循環(huán)
elif m == 9:
# 與上面差不多的算法
if data2[9] == data1[8]:
# 如果相等
N += 1
if (data2[9] in low_list and data3[9] in low_list) or (data2[9] in high_list and data3[9] in high_list):
n += 1
list_data_number.append(record_number)
if f"{record_number}" in dict_time_record.keys():
dict_time_record[f"{record_number}"].append(new_result3['preDrawTime'][11:])
else:
dict_time_record[f"{record_number}"] = [new_result3['preDrawTime'][11:]]
record_number = 1
else:
record_number += 1
break
else:
# 與上面差不多的算法
if data2[m] == data1[m + 1] or data2[m] == data1[m - 1]:
# 如果相等
N += 1
if (data2[m] in low_list and data3[m] in low_list) or (data2[m] in high_list and data3[m] in high_list):
n += 1
list_data_number.append(record_number)
if f"{record_number}" in dict_time_record.keys():
dict_time_record[f"{record_number}"].append(new_result3['preDrawTime'][11:])
else:
dict_time_record[f"{record_number}"] = [new_result3['preDrawTime'][11:]]
record_number = 1
else:
record_number += 1
break
print(f"日期:{new_response['result']['data'][0]['preDrawTime'][:10]},總的梯子數(shù)為{N}個(gè),一共有{n}次命中,一共有{N - n}次掛了")
# 打印時(shí)間,以及,可以購(gòu)買(mǎi)的期數(shù),命中的期數(shù),沒(méi)有命中的期數(shù)
list_data_number.sort()
# 按照大小順序來(lái)進(jìn)行排序
dict_record = {}
# 設(shè)置空字典進(jìn)行記錄
for i in list_data_number:
if f"{i}" in dict_record.keys(): # 判斷是否已經(jīng)有了這個(gè)數(shù)字?
dict_record[f"{i}"] += 1
# 如果有的話,那么就會(huì)自加一
else: # 如果沒(méi)有的話,那么就會(huì)創(chuàng)建并且賦值等于 1
dict_record[f"{i}"] = 1
# 創(chuàng)建一個(gè)新的字典元素,然后進(jìn)行賦值為 1
for j in dict_record.keys():
if (int(j) >= 6) and (int(j) 15):
# 實(shí)際的結(jié)果表明,我們需要的是大于等于6期的數(shù)據(jù),而沒(méi)有出現(xiàn)大于15的數(shù)據(jù),因此有這樣的一個(gè)關(guān)系式
print(f"買(mǎi){j}次才中獎(jiǎng)的次數(shù)為{dict_record[j]}")
# 打印相關(guān)信息
print(dict_time_record[j])
str0 = ""
for letter in dict_time_record[j]:
str0 += letter
str0 += ", "
print(str0)
# 打印相關(guān)信息
運(yùn)行結(jié)果的展示如下圖所示:
2.3 循環(huán)日期進(jìn)行多天的數(shù)據(jù)分析:
首先設(shè)置一個(gè)事件列表來(lái)記錄需要統(tǒng)計(jì)哪些天的數(shù)據(jù):
代碼:
data_list = []
for h in range(31):
data_list.append(f'1-{h + 1}')
for h in range(28):
data_list.append(f'2-{h + 1}')
for h in range(31):
data_list.append(f'3-{h + 1}')
for h in range(20):
data_list.append(f'4-{h + 1}')
通過(guò)上述的代碼,我們即實(shí)現(xiàn)了時(shí)間列表的設(shè)置,然后我們循環(huán)遍歷這個(gè)列表訪問(wèn)不同日期的彩票數(shù)據(jù)即就是得到了不同時(shí)間的數(shù)據(jù),然后再利用上述的分析方法來(lái)進(jìn)行數(shù)據(jù)分析,即就是可以得到了多天的彩票數(shù)據(jù)分析的結(jié)果了。
2.4 將數(shù)據(jù)寫(xiě)入Excel表格中
這里我們可以采用xlwt 模塊來(lái)進(jìn)行excel表格的寫(xiě)入操作啦,具體的寫(xiě)入就不必過(guò)多贅述了。
三、完整代碼
以下是完整的代碼:
import requests
import chardet
import json
import xlwt # excel 表格數(shù)據(jù)處理的對(duì)應(yīng)模塊
def reverse_list(lst):
"""
準(zhǔn)換列表的先后順序
:param lst: 原始列表
:return: 新的列表
"""
return [ele for ele in reversed(lst)]
data_list = []
for h in range(31):
data_list.append(f'1-{h + 1}')
for h in range(28):
data_list.append(f'2-{h + 1}')
for h in range(31):
data_list.append(f'3-{h + 1}')
for h in range(20):
data_list.append(f'4-{h + 1}')
wb = xlwt.Workbook() # 創(chuàng)建 excel 表格
sh = wb.add_sheet('彩票分析數(shù)據(jù)處理') # 創(chuàng)建一個(gè) 表單
sh.write(0, 0, "日期")
sh.write(0, 1, "梯子數(shù)目")
sh.write(0, 2, "命中數(shù)目")
sh.write(0, 3, "掛的數(shù)目")
sh.write(0, 4, "6次中的數(shù)目")
sh.write(0, 5, "6次中的時(shí)間")
sh.write(0, 6, "7次中的數(shù)目")
sh.write(0, 7, "7次中的時(shí)間")
sh.write(0, 8, "8次中的數(shù)目")
sh.write(0, 9, "8次中的時(shí)間")
sh.write(0, 10, "9次中的數(shù)目")
sh.write(0, 11, "9次中的時(shí)間")
sh.write(0, 12, "10次中的數(shù)目")
sh.write(0, 13, "10次中的時(shí)間")
sh.write(0, 14, "11次中的數(shù)目")
sh.write(0, 15, "11次中的時(shí)間")
sh.write(0, 16, "12次中的數(shù)目")
sh.write(0, 17, "12次中的時(shí)間")
sh.write(0, 18, "13次中的數(shù)目")
sh.write(0, 19, "13次中的時(shí)間")
sh.write(0, 20, "14次中的數(shù)目")
sh.write(0, 21, "14次中的時(shí)間")
# wb.save('test4.xls')
sheet_seek_position = 1
# 設(shè)置表格的初始位置為 1
for data in data_list:
low_list = ["01", "02", "03", "04", "05"]
high_list = ["06", "07", "08", "09", "10"]
N = 0
n = 0
url = f'https://api.api68.com/pks/getPksHistoryList.do?date=2021-{data}lotCode=10037'
headers = {
'User-Agent':
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) '
'AppleWebKit/537.36 (KHTML, like Gecko) '
'Chrome/90.0.4430.72 Safari/537.36'
}
response = requests.get(url=url, headers=headers)
response.encoding = chardet.detect(response.content)['encoding']
new_response = json.loads(response.text)
sh.write(sheet_seek_position, 0, new_response['result']['data'][0]['preDrawTime'][:10])
# 在表格的第一個(gè)位置處寫(xiě)入時(shí)間,意即:data
record_number = 1 # 記錄數(shù)據(jù)的一個(gè)判斷值,設(shè)置為第一次,應(yīng)該是要放在最外面的啦
list_data_number = []
# 設(shè)置一個(gè)空列表來(lái)存儲(chǔ)一天之中的連續(xù)掛的期數(shù)
dict_time_record = {}
for k in range(1152):
# record_number = 1,應(yīng)該要放外面
# 記錄數(shù)據(jù)的一個(gè)判斷值,設(shè)置為第一次
if k 1150:
new_result1 = reverse_list(new_response["result"]["data"])[k]
new_result2 = reverse_list(new_response["result"]["data"])[k + 1]
new_result3 = reverse_list(new_response["result"]["data"])[k + 2]
data1 = new_result1['preDrawCode'].split(',')
data2 = new_result2['preDrawCode'].split(',')
data3 = new_result3['preDrawCode'].split(',')
for m in range(10):
if m == 0:
if data2[0] == data1[1]:
N += 1
if (data2[0] in low_list and data3[0] in high_list) or (data2[0] in high_list and data3[0] in low_list):
n += 1
# 如果命中了的話,本輪結(jié)束,開(kāi)啟下一輪
list_data_number.append(record_number)
if f"{record_number}" in dict_time_record.keys():
dict_time_record[f"{record_number}"].append(new_result3['preDrawTime'][11:])
else:
dict_time_record[f"{record_number}"] = [new_result3['preDrawTime'][11:]]
# print(record_number)
record_number = 1 # 初始化
else:
record_number += 1 # 沒(méi)中,次數(shù)加一
# 自加一
break
elif m == 9:
if data2[9] == data1[8]:
N += 1
if (data2[9] in low_list and data3[9] in high_list) or (data2[9] in high_list and data3[9] in low_list):
n += 1
list_data_number.append(record_number)
if f"{record_number}" in dict_time_record.keys():
dict_time_record[f"{record_number}"].append(new_result3['preDrawTime'][11:])
else:
dict_time_record[f"{record_number}"] = [new_result3['preDrawTime'][11:]]
# print(record_number)
record_number = 1
else:
record_number += 1
break
else:
if data2[m] == data1[m + 1] or data2[m] == data1[m - 1]:
N += 1
if (data2[m] in low_list and data3[m] in high_list) or (data2[m] in high_list and data3[m] in low_list):
n += 1
list_data_number.append(record_number)
if f"{record_number}" in dict_time_record.keys():
dict_time_record[f"{record_number}"].append(new_result3['preDrawTime'][11:])
else:
dict_time_record[f"{record_number}"] = [new_result3['preDrawTime'][11:]]
# print(record_number)
record_number = 1
else:
record_number += 1
break
print(f"日期:{new_response['result']['data'][0]['preDrawTime'][:10]},總的梯子數(shù)為{N}個(gè),一共有{n}次命中,一共有{N - n}次掛了")
sh.write(sheet_seek_position, 1, N)
sh.write(sheet_seek_position, 2, n)
sh.write(sheet_seek_position, 3, N - n)
# new_list_data_number = list_data_number.sort()
list_data_number.sort()
# 進(jìn)行排序
dict_record = {}
# 設(shè)置空字典
for i in list_data_number:
if f"{i}" in dict_record.keys(): # 判斷是否已經(jīng)有了這個(gè)數(shù)字?
dict_record[f"{i}"] += 1
# 如果有的話,那么就會(huì)自加一
else: # 如果沒(méi)有的話,那么就會(huì)創(chuàng)建并且賦值等于 1
dict_record[f"{i}"] = 1
# 創(chuàng)建一個(gè)新的字典元素,然后進(jìn)行賦值為 1
# print(dict_record)
# print(f"買(mǎi)彩票第幾次才中獎(jiǎng)?")
# print(f"按照我們的規(guī)律買(mǎi)彩票的情況:")
for j in dict_record.keys():
if (int(j) >= 6) and (int(j) 15):
print(f"買(mǎi){j}次才中獎(jiǎng)的次數(shù)為{dict_record[j]}")
print(dict_time_record[j])
str0 = ""
for letter in dict_time_record[j]:
str0 += letter
str0 += ", "
print(str0)
sh.write(sheet_seek_position, 4 + (int(j) - 6) * 2, dict_record[j])
# 寫(xiě)入幾次
sh.write(sheet_seek_position, 4 + (int(j) - 6) * 2 + 1, str0[:-2])
# 注意這里應(yīng)該要改為 -2
# 寫(xiě)入幾次對(duì)應(yīng)的時(shí)間
# print(j)
sheet_seek_position += 1
# 每次寫(xiě)完了以后,要對(duì)位置進(jìn)行換行,換到下一行,從而方便下一行的寫(xiě)入
# 保存
wb.save('極速飛艇彩票分析結(jié)果.xls')
四、運(yùn)行結(jié)果
展示1、
展示2、
從而,我們便解決了極速飛艇的彩票的數(shù)據(jù)分析
然后,我們只需要稍稍改變一點(diǎn)點(diǎn)算法,其他的部分是完全一樣的啦,從而即就是可以實(shí)現(xiàn)極速賽車(chē)的數(shù)據(jù)分析了啦。
修改的代碼在下面列出來(lái)了:
for m in range(10):
if m == 0:
if data2[0] == data1[1]:
N += 1
if (data2[0] in low_list and data3[0] in low_list) or (data2[0] in high_list and data3[0] in high_list):
n += 1
# 如果命中了的話,本輪結(jié)束,開(kāi)啟下一輪
list_data_number.append(record_number)
if f"{record_number}" in dict_time_record.keys():
dict_time_record[f"{record_number}"].append(new_result3['preDrawTime'][11:])
else:
dict_time_record[f"{record_number}"] = [new_result3['preDrawTime'][11:]]
# print(record_number)
record_number = 1 # 初始化
else:
record_number += 1 # 沒(méi)中,次數(shù)加一
# 自加一
break
elif m == 9:
if data2[9] == data1[8]:
N += 1
if (data2[9] in low_list and data3[9] in low_list) or (data2[9] in high_list and data3[9] in high_list):
n += 1
list_data_number.append(record_number)
if f"{record_number}" in dict_time_record.keys():
dict_time_record[f"{record_number}"].append(new_result3['preDrawTime'][11:])
else:
dict_time_record[f"{record_number}"] = [new_result3['preDrawTime'][11:]]
# print(record_number)
record_number = 1
else:
record_number += 1
break
else:
if data2[m] == data1[m + 1] or data2[m] == data1[m - 1]:
N += 1
if (data2[m] in low_list and data3[m] in low_list) or (data2[m] in high_list and data3[m] in high_list):
n += 1
list_data_number.append(record_number)
if f"{record_number}" in dict_time_record.keys():
dict_time_record[f"{record_number}"].append(new_result3['preDrawTime'][11:])
else:
dict_time_record[f"{record_number}"] = [new_result3['preDrawTime'][11:]]
# print(record_number)
record_number = 1
else:
record_number += 1
break
五、總結(jié)
總而言之,這個(gè)就是我學(xué)習(xí) Python 到現(xiàn)在為止所接的第一單啦,這個(gè)需求不得不說(shuō)確實(shí)是比較簡(jiǎn)單的啦,但是,我在完成這個(gè)任務(wù)的過(guò)程中,的確是學(xué)到了以前一些我并不太注意的東西,同時(shí)呢,也熟練的掌握了一些編程的技巧,雖然說(shuō)這種比較簡(jiǎn)單的活報(bào)酬不會(huì)太高,但是,我認(rèn)為在這個(gè)過(guò)程中我確實(shí)學(xué)到了不少東西,同時(shí)也成長(zhǎng)了不少,因此,無(wú)論怎么說(shuō),其實(shí)也都還是挺不錯(cuò)的啦。
到此這篇關(guān)于Python數(shù)據(jù)分析之彩票的歷史數(shù)據(jù)的文章就介紹到這了,更多相關(guān)python分析彩票數(shù)據(jù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- 用Python 爬取貓眼電影數(shù)據(jù)分析《無(wú)名之輩》
- python數(shù)據(jù)分析之用sklearn預(yù)測(cè)糖尿病
- 詳解python爬取彈幕與數(shù)據(jù)分析
- Python爬取用戶觀影數(shù)據(jù)并分析用戶與電影之間的隱藏信息!