Python - 在其他两个日期之间生成 k 个随机日期
给定两个日期,任务是编写一个Python程序来随机获取 K 个日期。
Input : test_date1, test_date2 = date(2015, 6, 3), date(2015, 7, 1), K = 7
Output : [datetime.date(2015, 6, 18), datetime.date(2015, 6, 25), datetime.date(2015, 6, 29), datetime.date(2015, 6, 11), datetime.date(2015, 6, 11), datetime.date(2015, 6, 10), datetime.date(2015, 6, 24)]
Explanation : 7 random dates starting from 3 June 2015 to 1 July 2015 are generated.
Input : test_date1, test_date2 = date(2015, 6, 3), date(2015, 8, 1), K = 6
Output : [datetime.date(2015, 6, 20), datetime.date(2015, 7, 22), datetime.date(2015, 6, 29), datetime.date(2015, 6, 18), datetime.date(2015, 6, 13), datetime.date(2015, 7, 14)]
Explanation : 6 random dates starting from 3 June 2015 to 1 August 2015 are generated.
方法#1:使用choices() + loop + timedelta()
在此,我们使用 1 天差异的循环和 timedelta() 提取范围内的所有日期。其中 K 个随机日期是使用choice() 选择的。
Python3
# Python3 code to demonstrate working of
# Random K dates in Range
# Using choices() + timedelta() + loop
from datetime import date, timedelta
from random import choices
# initializing dates ranges
test_date1, test_date2 = date(2015, 6, 3), date(2015, 7, 1)
# printing dates
print("The original range : " + str(test_date1) + " " + str(test_date2))
# initializing K
K = 7
res_dates = [test_date1]
# loop to get each date till end date
while test_date1 != test_date2:
test_date1 += timedelta(days=1)
res_dates.append(test_date1)
# random K dates from pack
res = choices(res_dates, k=K)
# printing
print("K random dates in range : " + str(res))
Python3
# Python3 code to demonstrate working of
# Random K dates in Range
# Using randrange() + timedelta() + loop
from datetime import date, timedelta
import random
# initializing dates ranges
test_date1, test_date2 = date(2015, 6, 3), date(2015, 7, 1)
# printing dates
print("The original range : " + str(test_date1) + " " + str(test_date2))
# initializing K
K = 7
# getting days between dates
dates_bet = test_date2 - test_date1
total_days = dates_bet.days
res = []
for idx in range(K):
random.seed(a=None)
# getting random days
randay = random.randrange(total_days)
# getting random dates
res.append(test_date1 + timedelta(days=randay))
# printing
print("K random dates in range : " + str(res))
输出:
The original range : 2015-06-03 2015-07-01
K random dates in range : [datetime.date(2015, 6, 25), datetime.date(2015, 6, 10), datetime.date(2015, 6, 18), datetime.date(2015, 6, 7), datetime.date(2015, 6, 4), datetime.date(2015, 6, 16), datetime.date(2015, 6, 12)]
方法 #2:使用randrange() + timedelta() + 循环
在这种情况下,提取范围之间的总天数,该范围用于获取元素 K 次,从使用 randrange() 提取的开始日期添加一个随机索引数。
蟒蛇3
# Python3 code to demonstrate working of
# Random K dates in Range
# Using randrange() + timedelta() + loop
from datetime import date, timedelta
import random
# initializing dates ranges
test_date1, test_date2 = date(2015, 6, 3), date(2015, 7, 1)
# printing dates
print("The original range : " + str(test_date1) + " " + str(test_date2))
# initializing K
K = 7
# getting days between dates
dates_bet = test_date2 - test_date1
total_days = dates_bet.days
res = []
for idx in range(K):
random.seed(a=None)
# getting random days
randay = random.randrange(total_days)
# getting random dates
res.append(test_date1 + timedelta(days=randay))
# printing
print("K random dates in range : " + str(res))
输出:
The original range : 2015-06-03 2015-07-01
K random dates in range : [datetime.date(2015, 6, 26), datetime.date(2015, 6, 5), datetime.date(2015, 6, 6), datetime.date(2015, 6, 18), datetime.date(2015, 6, 21), datetime.date(2015, 6, 15), datetime.date(2015, 6, 12)]