简体   繁体   English

遍历字典 x 次

[英]Iterating through a dictionary x number of times

To start, I have a 2 variables to consider: offer_duration and offer_start_day .首先,我需要考虑 2 个变量: offer_durationoffer_start_day An offer can start on any day of the week and the duration is variable (between 1 and 7 days).报价可以在一周中的任何一天开始,持续时间是可变的(1 到 7 天之间)。

I am trying to create a list of all the days of the week that the offer ran on.我正在尝试创建报价运行的一周中所有日期的列表。 ie if offer start day is on a Thursday and lasts for 3 days, I need a list outputting the following: ['Thursday', 'Friday', 'Saturday'].即如果报价开始日是星期四并持续 3 天,我需要一个输出以下内容的列表:['Thursday'、'Friday'、'Saturday']。

So far I have created a dictionary:到目前为止,我已经创建了一个字典:

day_of_week = {"Monday":0, "Tuesday":1, "Wednesday":2, "Thursday":3, "Friday":4, "Saturday":5, "Sunday":6}

I am trying to iterate through the dictionary starting on the day of the week (ie Thursday) and iterating offer_duration number of times.我正在尝试从一周中的某一天(即星期四)开始遍历字典并迭代 offer_duration 次数。 I have produced the following but do not know how to get to my solution or if using a dictionary for this problem is the correct approach.我已经制作了以下内容,但不知道如何找到我的解决方案,或者使用字典来解决这个问题是否是正确的方法。 any help would be appreciated任何帮助,将不胜感激

for key, num in day_of_week.items():
    if num == test_case('2019-06-27').weekday():
        starting_day = num
print(starting_day)

Dictionaries are not ordered, so you are not guaranteed that your day_of_week keys will get iterated over in the right order.字典没有排序,所以你不能保证你的 day_of_week 键会以正确的顺序迭代。 A better way to do this is to use the datetime module and timedelta.更好的方法是使用 datetime 模块和 timedelta。 Like this:像这样:

start = datetime.date(2019, 6, 27)
duration = 3  # days
days = [(start + datetime.timedelta(days=n)).strftime('%A') for n in range(duration)]
assert days == ['Thursday', 'Friday', 'Saturday']

To start I would initiate the day_of_week this way instead:首先,我会以这种方式启动day_of_week

day_of_week = {0: 'Monday', 1: 'Tuesday', 2: 'Wednesday', 3: 'Thursday', 4: 'Friday', 5: 'Saturday', 6: 'Sunday'}

you can achieve this from your current definition like so:您可以从您当前的定义中实现这一点,如下所示:

day_of_week = {v:k for k,v in day_of_week.items()}

then然后

import datetime

my_week_day = datetime.datetime(2019,6,27).weekday()
offer_length = 3

offer_days = [v for k,v in day_of_week.items() if k in [a%7 for a in range(my_week_day,my_week_day + offer_length)]]

print(offer_days)

Output: ['Thursday', 'Friday', 'Saturday'] Output: ['Thursday', 'Friday', 'Saturday']

Or you could use the below to maintain the correct day order或者您可以使用以下内容来维护正确的日期顺序


offer_days = []
for a in range(my_week_day,my_week_day + offer_length):
    offer_days.append(day_of_week[a%7])

print(offer_days)

I only imported datetime since I don't know your test_case function.我只导入了日期时间,因为我不知道您的 test_case function。

You can store the days of week in a list and then use a list comprehension:您可以将星期几存储在列表中,然后使用列表推导:

days_of_week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
number_of_days = 7
date = dt.date(2019, 6, 29)

>>> [days_of_week[n % 7] for n in range(date.weekday(), date.weekday() + number_of_days)]
['Thursday', 'Friday', 'Saturday', 'Sunday', 'Monday', 'Tuesday', 'Wednesday']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM