English 中文(简体)
在 Python 时间对象中绕圈周日
原标题:Looping over weekday in python time object

我有一套司机旅行日记的数据集,每次旅行都有Csv文件的开始时间、结束时间和一周的一天。没有旅行日期。

我现在把数据输入了皮松, 每一个开始时间和结束时间 都把周日附在它上,

time.struct_time(tm_year=1900, tm_mon=1, tm_mday=1, tm_hour=23, 
                 tm_min=45, tm_sec=0, tm_wday=0, tm_yday=1, tm_isdst=-1)

print journey[ BeginTime ][2].tm_wday, journey[ BeginTime ][2].tm_hour

返回周一0,小时23。

这些旅行中有11 000次,我想得到的,是按每天时间计算的每星期驾驶的汽车数量简况。

可以通过计算各自[初 和[末 间隔之间在特定时间间隔内的旅行次数来推断这一点。 5分钟间隔足够,因为数据最接近5分钟。

是否有一种优雅的比松方法来做这个?

for fiveMinutes in Week:
count = 0
    for trip in range(len(journey[ BeginTime ]):
        if journey[ BeginTime ][trip] == fiveMinutes
               or (journey[ BeginTime ][trip] < fiveMinutes 
                   and journey[ EndTime ][trip] > fiveMinutes):
           count = count + 1
carCount[fiveMinutes] = count
最佳回答

如果这有帮助,我有个想法...

from datetime import datetime, timedelta

# This does not check for crossing from Sunday to Monday
def convert_dt(start_dt, journey):
    begin_weekday, begin_hour, begin_minute = journey[0]
    end_weekday, end_hour, end_minute = journey[1]

    begin_dt = start_dt + timedelta(days=begin_weekday)
    begin_dt += timedelta(hours=begin_hour, minutes=begin_minute)

    end_dt = start_dt + timedelta(days=end_weekday)
    end_dt += timedelta(hours=end_hour,minutes=end_minute)
    return (begin_dt, end_dt)

def get_slot_journeys(start_dt, journeys):           
    next_dt = start_dt
    slot_count =  60/5 * 24 * 7
    slot_dict = {}

    journey_dts = []
    #convert journey begin and end to datetimes
    for index in range(len(journeys[ begin_weekday ])):
        next_journey = [(journeys[ begin_weekday ][index],
                         journeys[ begin_hour ][index],
                         journeys[ begin_minute ][index],),
                        (journeys[ end_weekday ][index],
                         journeys[ end_hour ][index],
                         journeys[ end_minute ][index],)
                       ]
        journey_dts.append(convert_dt(start_dt, next_journey))

    for slot in range(slot_count):
        slot_dict[next_dt] = 0
        for journey_start, journey_end in journey_dts:
            if next_dt >= journey_start and next_dt <= journey_end:
                slot_dict[next_dt] = slot_dict[next_dt] + 1                    

        next_dt += timedelta(minutes=(5))

    return slot_dict

if __name__ == "__main__":
    start_dt = datetime(2012, 1, 2, 0, 0)    

    journeys = { begin_weekday : [0, 0],
                 begin_hour : [14, 18],
                 begin_minute : [20, 30],
                 end_weekday : [0, 1],
                 end_hour : [19, 12],
                 end_minute : [15, 55],
               }
    slot_dict = get_slot_journeys(start_dt, journeys)       
    slot_keys = slot_dict.keys()
    slot_keys.sort()

    for key in slot_keys:
        if slot_dict[key]:    
            print key, slot_dict[key]
问题回答

暂无回答




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签