在Python中选择过去2年的随机月份

时间:2018-06-17 03:03:34

标签: python random

我想选择当年和2016年之间的随机月份。 这是我目前的非常天真的解决方案

from random import choice
def get_month():
    return choice({'2018-06','2018-05','2018-04','2018-03'})

很明显,这套装置将来会变得太大,那么更好的方法是什么呢?

8 个答案:

答案 0 :(得分:4)

可能你可以有两个月份和年份列表,这些将被修复。然后,您可以在每个之间随机选择并返回日期。这样我觉得不需要生成所有不同的日期,也不需要存储在大型列表中:

from random import choice
def get_month():
    months = list(range(1, 13)) # 12 months list
    year = list(range(2016, 2019)) # years list here
    val = '{}-{:02d}'.format(choice(year), choice(months))
    return val

get_month()

结果:

'2017-05'

更新

如果选择的年份是当前年份,如果有不超过当月的限制,您可能需要if条件来生成以下月份列表:

from random import choice
from datetime import datetime

def get_month():

    today = datetime.today() # get current date
    year = list(range(2016, today.year+1)) # list till current date year

    # randomly select year and create list of month based on year
    random_year = choice(year)

    # if current year then do not exceed than current month
    if random_year == today.year:
        months = list(range(1, today.month+1))
    else:
        # if year is not current year then it is less so can create 12 months list
        months = list(range(1, 13)) 

    val = '{}-{:02d}'.format(random_year, choice(months))

    return val

答案 1 :(得分:2)

您可以使用库pandas并使用date_range

choice(pd.date_range(start="2016-01-01", end="2018-01-01", freq='M'))

如果你想要它直到今天,你可以用startend参数替换任何合适的参数,例如。

from dateutil.relativedelta import relativedelta
today = datetime.today()
two_years_ago = today - relativedelta(years=2)
choice(pd.date_range(start=two_years_ago, end=today, freq='M'))

答案 2 :(得分:1)

您已正确识别问题。虽然这些离散项目很快就会变得笨拙(实际上它不会),但选择random integer over an arbitrary range很容易。

您应该能够轻松计算感兴趣日期之间的月数。这意味着你有一个从几个月到整数的简单的一对一映射。现在你可以做到

m = random.randrange(N)

其中N是您的总月数(当月数+ 12 *全年数)。使用Python的datetime API

可以轻松映射回数月
origin = datetime.date(2016, 1, 1)
today = datetime.today()
n = (today.year - origin.year) * 12 + today.month - origin.month

m = random.randrange(n)
x = origin.replace(year=origin.year + m // 12, month=origin.month + m % 12)

基于this answer的月增量(n)和基于this answer的月增量(x)。如果你从1月起离开原点,两种计算都会变得稍微复杂一些。

答案 3 :(得分:1)

随机选择1-24 然后应用一些日期算术来从当前日期开始。

from datetime import datetime, date
from dateutil.relativedelta import relativedelta
from random import randint

refdate = date.today() #datetime.now() works too!
for i in range(0,10):
    #this will take off at least 1 month and 24 will return 2016-06 from 2018-06
    months = randint(1,24)  
    back = refdate - relativedelta(months=months)
    print ("start:%s - %02d months => %s" % (refdate, months, back))

输出:

start:2018-06-17 - 24 months => 2016-06-17
start:2018-06-17 - 22 months => 2016-08-17
start:2018-06-17 - 21 months => 2016-09-17
start:2018-06-17 - 08 months => 2017-10-17
start:2018-06-17 - 16 months => 2017-02-17
start:2018-06-17 - 06 months => 2017-12-17
start:2018-06-17 - 07 months => 2017-11-17
start:2018-06-17 - 14 months => 2017-04-17
start:2018-06-17 - 07 months => 2017-11-17
start:2018-06-17 - 07 months => 2017-11-17

答案 4 :(得分:0)

你可以单独做它们,比如随机抽取一年,然后随机一个月,然后把它放在一起

def get_month():
    year = random.choice([2016, 2017, 2018])
    month = random.choice(range(1,13))
    new_date = str(year) + "-" + str(month)
    return new_date

答案 5 :(得分:0)

尝试使用calendar

from random import choice
import calendar
def get_month():
    l = []
    y = 0
    for i in range(2016,2019):
        l.append(str(i)+'-'+str(calendar.monthrange(i,12)[0]))
    return choice(l)
print(get_month())

答案 6 :(得分:0)

我认为我使用的是比使用pandas

更轻的解决方案
current_month = datetime.today().month
current_year = datetime.today().year 
start_year = 2016

#total months between now and the start year
total_months = current_month + (current_year - start_year)*12 

months = []
month_count = 1
year_count = start_year

#iterate through the list, when it reaches 13, increment year_count and reset the month_count to 1
for month in range(total_months):
    if month_count<13:
        months.append(str(year_count) + "-" + str("{0:0=2d}".format(month_count)))
        month_count+=1
    if month_count == 13:
        year_count+=1
        month_count=1

结果

['2016-01', '2016-02', '2016-03', '2016-04', '2016-05', '2016-06', '2016-07', '2016-08', '2016-09', '2016-10', '2016-11', '2016-12', '2017-01', '2017-02', '2017-03', '2017-04', '2017-05', '2017-06', '2017-07', '2017-08', '2017-09','2017-10', '2017-11', '2017-12', '2018-01', '2018-02', '2018-03', '2018-04', '2018-05', '2018-06']

出于某种原因,当我更换&#34;如果month_count == 13&#34;与&#34; else&#34;,它只会到2018-04

我很感激有关此解决方案的一些反馈。

答案 7 :(得分:0)

我希望我的代码会给你一些灵感。

 import random
 import datetime

 def RandomMonth(st_year, st_month, ed_year, ed_month):
     """st_year, st_month and ed_year/month is int. """
     start_date = datetime.datetime(st_year, st_month, 1)
     end_date = datetime.datetime(ed_year, ed_month, 31)
     d_days = (end_date - start_date).days
     r_days = random.randint(0, ttl_days)
     r_date = start_date + datetime.timedelta(days = r_days)
     return r_date.strftime("%B/%Y")