18luck网站

18luck网站電子設計 | 18luck网站Rss 2.0 會員中心 會員注冊
搜索: 您現在的位置: 18luck网站 >> 編程學習 >> Python >> 正文

用Python實現一個簡單的抽獎小程序

作者:佚名    文章來源:本站原創    點擊數:    更新時間:2023/5/19

寫在前麵 

因為(wei) 粉絲(si) 福利,所以想自己寫(xie) 一個(ge) 抽獎小程序,用於(yu) 實現粉絲(si) 抽獎結果。

原本規劃的比較理想:

實時拉取評論用戶信息數據清洗,數據去重存儲(chu) 評論用戶信息前端酷炫的展示效果

實現結果 

哈哈,但是理想是豐(feng) 滿的,現實是骨感的,加上最近比較忙,就簡單寫(xie) 了一下,實現了:

用戶數據拉取數據清洗,數據去重中獎數據抽取

總體(ti) 算後端實現了50%吧,前端0%  ,哈哈~~~後麵有時間了再進一步吧!😁😁😁

1 數據拉取

用Python 實現的,以下為(wei) 相關(guan) 代碼。使用過程中,有點報錯,最終使用postman,手動獲取的,

哈哈哈~~~後麵再優(you) 化吧

import json import requests def request_get(url, param): fails = 0 while True: try: if fails >= 20: break ret = requests.get(url=url, params=param, timeout=10) if ret.status_code == 200: text = json.loads(ret.text) else: continue except: fails += 1 print('網絡連接出現問題, 正在嚐試再次請求: ', fails) else: break return text def request_post(url, param): fails = 0 while True: try: if fails >= 20: break # headers = {'content-type': 'application/json'} ret = requests.post(url, json=param, timeout=10) if ret.status_code == 200: text = json.loads(ret.text) else: continue except: fails += 1 print('網絡連接出現問題, 正在嚐試再次請求: ', fails) else: break return text

數據接口不適合放出來,就不放啦~😁

用戶數據不知道合不合適放出來,也就不放啦~😁

2 數據清洗、去重

因為(wei) 我原本想的是要做一個(ge) 酷炫的前端,也有找到了一些不錯的案例,3D抽獎的效果,需要用戶頭像,就在清洗數據的同時,也存儲(chu) 了用戶頭像的鏈接,以下是完整代碼:

import json def getData(): with open("data.json", 'r') as f: temp = json.loads(f.read()) templist = temp['data']['list'] user = [] for i in templist: temp={ 'nickName':i['info']['nickName'], 'avatar':i['info']['avatar'] } user.append(temp) result = [dict(t) for t in set([tuple(d.items()) for d in user])] print(result) print(len(result)) return result if __name__ == '__main__': getData()

這一步是重複數據去重 

result = [dict(t) for t in set([tuple(d.items()) for d in user])]

3 中獎數據抽取

中獎名單抽取---完整代碼

import random from giftchoose.userData import getdata def getgift(): num = getdata() # 用於(yu) 存放抽獎用戶信息 print("歡迎來到抽獎小程序!") print("參與(yu) 抽獎的用戶如下:") for i in num: print(i) usernum = len(num) reward = 2 usernum = int(usernum) reward = int(reward) resultList = [] # 用於(yu) 存放隨機數結果 def generateRand(counter): tempInt = random.randint(0, usernum-1) # 生成一個(ge) 範圍內(nei) 的臨(lin) 時隨機數 if(counter <= reward): # 先看隨機數的總個(ge) 數是不是夠了,如果不夠 if(tempInt not in resultList): # 再檢查當前已經生成的臨(lin) 時隨機數是不是已經存在 resultList.append(tempInt) # 如果不存在,則將其追加到結果resultList中 counter += 1 # 然後將表示有效結果的個(ge) 數加1 generateRand(counter) # 不管上麵的if是否成立,都要遞歸。 generateRand(1) # 調用遞歸函數,並給當前要生成的有效隨機數的個(ge) 序號置為(wei) 1 prize = [] # 用於(yu) 存放中獎號碼 # 將中獎號碼添加到結果prize中 for j in range(reward): prize.append(num[resultList[j]]) print("\n中獎用戶:") # 輸出中獎結果 for k in range(reward): print(prize[k], end="\n") if __name__ == '__main__': getgift()

項目結構,太簡單了,其實也算不上項目了😁😁😁😁😁😁

 

python 相關知識點

list列表去重的方案

循環遍曆

old_list = [10, 1, 1, 2, 3, 4, 5, 1, 2, 3, 6, 7, 8, 8, 9, 10, 10] def one(): new_list = [] for i in old_list: if i not in new_list: new_list.append(i) print(new_list)

使用dict:

使用list項作為(wei) 鍵創建dict,會(hui) 自動刪除任何重複項,保留原順序,因為(wei) dict不能有重複的鍵。

old_list = [10, 1, 1, 2, 3, 4, 5, 1, 2, 3, 6, 7, 8, 8, 9, 10, 10] def useDict(): new_list = list(dict.fromkeys(old_list)) print(new_list)

使用set:將list轉化為(wei) set再轉化為(wei) list,set有自動去重功能,缺點:新的list不保證原順序。

def useSet(): new_list = list(set(old_list)) print(new_list)

使用set 並保證順序:不能保證順序,咱就給他加個(ge) 原來的key,這樣不就保障了嘛~

def keepOrder(): new_list = list(set(old_list)) new_list.sort(key=old_list.index) print(new_list)

運行結果

 

總結

到此這篇關(guan) 於(yu) 用Python實現一個(ge) 簡單的抽獎小程序的文章就介紹到這了

Tags:python,編程學習  
責任編輯:admin
  • 上一篇文章:
  • 下一篇文章:
  • 請文明參與討論,禁止漫罵攻擊。 昵稱:注冊  登錄
    [ 查看全部 ] 網友評論
    關於我們 - 聯係我們 - 廣告服務 - 友情鏈接 - 網站地圖 - 版權聲明 - 在線幫助 - 文章列表
    返回頂部
    刷新頁麵
    下到頁底
    晶體管查詢