この記事では、指定された期間内でランダムで非繰り返しの時点を取得するための Python の実装コードを紹介します
シナリオ 1: 07:30:00 ~ 09:30:33 の間で N 個のランダムな時間を取得します。
以下は私のコードです:
#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 までの時間の秒を生成し、次を使用します。ランダム サンプルはそこから 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 2020 年 1 月 1 日の 0:00:00 からの合計秒数。 秒に変換すると、任意の 1 日の 24 時間以内の時刻は、1970 年 1 月 1 日 (協定世界時) のタイムスタンプとまったく同じになります。したがって、必要に応じて、プログラミング言語組み込みのタイムスタンプ関数を使用して変換できます。
以上が指定された期間内でランダムな非反復時点を取得するための Python 実装コードの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

Pythonlistscanstoreanydatatype,arraymodulearraysstoreonetype,andNumPyarraysarefornumericalcomputations.1)Listsareversatilebutlessmemory-efficient.2)Arraymodulearraysarememory-efficientforhomogeneousdata.3)NumPyarraysareoptimizedforperformanceinscient

heouttemptemptostoreavure ofthewrongdatatypeinapythonarray、yure counteractypeerror.thisduetothearraymodule'sstricttypeeencultionyを使用します

PythonListSarePartOfThestAndardarenot.liestareBuilting-in、versatile、forStoringCollectionsのpythonlistarepart。

theScriptisrunningwithwrongthonversionduetorectRectDefaultEntertersettings.tofixthis:1)CheckthedededefaultHaulthonsionsingpython - versionorpython3-- version.2)usevirtualenvironmentsbycreatingonewiththon3.9-mvenvmyenv、andverixe

PythonArraysSupportVariousoperations:1)SlicingExtractsSubsets、2)Appending/ExtendingAdddesements、3)inSertingSelementSatspecificpositions、4)remvingingDeletesements、5)sorting/verversingsorder、and6)listenionsionsionsionsionscreatenewlistsebasedexistin

numpyarraysAressertialentionsionceivationsefirication-efficientnumericalcomputations andDatamanipulation.theyarecrucialindatascience、mashineelearning、物理学、エンジニアリング、および促進可能性への適用性、scaledatiencyを効率的に、forexample、infinancialanalyyy

UseanArray.ArrayOverAlistinPythonは、Performance-criticalCode.1)homogeneousdata:araysavememorywithpedelements.2)Performance-criticalcode:Araysofterbetterbetterfornumerumerumericaleperations.3)interf

いいえ、notallistoperationSaresuptedbyarrays、andviceversa.1)arraysdonotsupportdynamicoperationslikeappendorintorintorinsertizizing、whosimpactsporformance.2)リスト


ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

AtomエディタMac版ダウンロード
最も人気のあるオープンソースエディター

ドリームウィーバー CS6
ビジュアル Web 開発ツール

EditPlus 中国語クラック版
サイズが小さく、構文の強調表示、コード プロンプト機能はサポートされていません

Dreamweaver Mac版
ビジュアル Web 開発ツール

SublimeText3 英語版
推奨: Win バージョン、コードプロンプトをサポート!

ホットトピック









