這篇文章介紹python取得指定時間段內的隨機不重複時間點的實作程式碼
場景1:取N個07:30: 00-09:30:33之間的隨機時間。
下面是我的程式碼:
#2016-12-10 7:06:29 codegay import random st = "07:30:00" et = "09:30:33" def time2seconds(t): h,m,s = t.strip().split(":") return int(h) * 3600 + int(m) * 60 + int(s) def seconds2time(sec): m,s = pmod(sec,60) h,m = pmod(m,60) return "%02d:%02d:%02d" % (h,m,s) sts = time2seconds(st) #sts==27000 ets = time2seconds(et) #ets==34233 rt = random.sample(range(sts,ets),10) #rt == [28931, 29977, 33207, 33082, 31174, 30200, 27458, 27434, 33367, 30450] rt.sort() #对时间从小到大排序 for r in rt: print(seconds2time(r)) """ 输出: 07:43:12 07:54:31 08:08:33 08:27:46 08:46:53 08:48:17 08:55:20 08:59:16 09:10:23 09:15:58 """
從程式碼中可以發現思路是把時間轉成秒數後,那麼就可以用range生07:30-09:30之間的時間秒數,再用random.sample從中取出個N個秒數,最後再把秒數轉換成所需的時間格式。
場景2: 時間大小比較和時間範圍的判斷
>>> "09:30:00" > "9:30:00" False >>> "09:30:00" == "9:30:00" False
基於字串的判斷可能會出現像上面的情況,我感覺統一轉成數字後再計算更可靠。
場景3:時間轉成秒數後剛好等於1970年1月1日的時間戳
UNIX時間,或稱為POSIX時間是UNIX或類別UNIX系統所使用的時間表示方式:從協調世界時1970年1月1日0時0分0秒起至現在的總秒數。
任當天24小時內的時間轉成秒數後都剛好等於UTC 1970年1月1日的時間戳記。所以有需要的話可以使用程式語言內建的時間戳函數來轉換。
以上是python取得指定時間段內的隨機不重複時間點的實作程式碼的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Arraysinpython,尤其是Vianumpy,ArecrucialInsCientificComputingfortheireftheireffertheireffertheirefferthe.1)Heasuedfornumerericalicerationalation,dataAnalysis和Machinelearning.2)Numpy'Simpy'Simpy'simplementIncressionSressirestrionsfasteroperoperoperationspasterationspasterationspasterationspasterationspasterationsthanpythonlists.3)inthanypythonlists.3)andAreseNableAblequick

你可以通過使用pyenv、venv和Anaconda來管理不同的Python版本。 1)使用pyenv管理多個Python版本:安裝pyenv,設置全局和本地版本。 2)使用venv創建虛擬環境以隔離項目依賴。 3)使用Anaconda管理數據科學項目中的Python版本。 4)保留系統Python用於系統級任務。通過這些工具和策略,你可以有效地管理不同版本的Python,確保項目順利運行。

numpyarrayshaveseveraladagesoverandastardandpythonarrays:1)基於基於duetoc的iMplation,2)2)他們的aremoremoremorymorymoremorymoremorymoremorymoremoremory,尤其是WithlargedAtasets和3)效率化,效率化,矢量化函數函數函數函數構成和穩定性構成和穩定性的操作,製造

數組的同質性對性能的影響是雙重的:1)同質性允許編譯器優化內存訪問,提高性能;2)但限制了類型多樣性,可能導致效率低下。總之,選擇合適的數據結構至關重要。

到CraftCraftExecutablePythcripts,lollow TheSebestPractices:1)Addashebangline(#!/usr/usr/bin/envpython3)tomakethescriptexecutable.2)setpermissionswithchmodwithchmod xyour_script.3)

numpyArraysareAreBetterFornumericalialoperations andmulti-demensionaldata,而learthearrayModuleSutableforbasic,內存效率段

numpyArraySareAreBetterForHeAvyNumericalComputing,而lelethearRayModulesiutable-usemoblemory-connerage-inderabledsswithSimpleDatateTypes.1)NumpyArsofferVerverVerverVerverVersAtility andPerformanceForlargedForlargedAtatasetSetsAtsAndAtasEndCompleXoper.2)

ctypesallowscreatingingangandmanipulatingc-stylarraysinpython.1)usectypestoInterfacewithClibrariesForperfermance.2)createc-stylec-stylec-stylarraysfornumericalcomputations.3)passarraystocfunctions foreforfunctionsforeffortions.however.however,However,HoweverofiousofmemoryManageManiverage,Pressiveo,Pressivero


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

SublimeText3 Linux新版
SublimeText3 Linux最新版

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能