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

问题描述 投票:3回答:8

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

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

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

python random
8个回答
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'

Update

如果如果所选年份是当前年份,如果有不超过当月的限制,您可能需要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

2
投票

您可以使用库pandas并使用date_range

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

如果你想要它直到今天,你可以用startand和end参数代替任何合适的参数,例如:

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'))

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)

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


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

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

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())

0
投票

我认为我使用的是比使用熊猫更轻的解决方案

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']

出于某种原因,当我用“else”替换“if month_count == 13”时,它只会到2018-04

我将很感激对此解决方案的一些反馈。


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")
© www.soinside.com 2019 - 2024. All rights reserved.