棘手的python 2d列表操作

时间:2018-08-15 06:34:46

标签: python list

我有这样的二维列表

s = [

    [1, '2017-08-15 13:30:00', 'pickup'],

    [1, '2017-08-15 14:00:00', 'dropoff'],

    [2, '2017-08-15 14:30:00', 'pickup'],

    [3, '2017-08-15 14:35:00', 'pickup'],

    [2, '2017-08-15 15:00:00', 'dropoff'],

    [4, '2017-08-15 15:05:00', 'pickup'],

    [3, '2017-08-15 15:10:00', 'dropoff'],

    [4, '2017-08-15 15:40:00', 'dropoff'],

]

我正在尝试创建一个程序,该程序将计算该人活动的时间段,即他实际持有至少一个包裹的时间。我无法弄清楚应该对重叠包应用什么逻辑,因为同一时间段将被多次添加。任何帮助表示赞赏。

编辑: 这是我目前拥有的代码

for i in range(len(s)-1):
    if s[i+1][0] == s[i][0]:
        print(s[i])
        diff = datetime.datetime.strptime(s[i+1][1], datetimeFormat) - datetime.datetime.strptime(s[i][1], datetimeFormat)
        print(diff)

我可以得到两次之间的差,但我需要处理重叠的时间。

3 个答案:

答案 0 :(得分:3)

我刚刚快速输入了一些内容,也许您从这里得到了一个主意

from datetime import datetime, date
# stores each time fram between pickup and dropoff
time = []
# inits a global counter, that is updated dependend on you array
counter = 0
# inits global time constants that are updated, based on logic
starttime = datetime(2009, 10, 5, 18, 0)
endtime = datetime(2009, 10, 5, 18, 0)

for i in range(len(s)):
    # check if initial pickup, if 0 that means inital pickup, therefore update time
    if(counter == 0):
        starttime = datetime.strptime(s[i][1],'%Y-%m-%d %H:%M:%S')
    # in case pickup add 1 to counter to indicate the number of packages on hand
    if(s[i][2] == 'pickup'):
        counter = counter + 1
    # in case dropoff subtract 1 from counter to update packages on hand
    else:
        counter = counter - 1
    # in case counter reaches 0 meaning no more packages, therefore measure time differences
    if(counter == 0):
        endtime = datetime.strptime(s[i][1],'%Y-%m-%d %H:%M:%S')
        print(endtime - starttime)
        time.append(endtime - starttime)

答案 1 :(得分:1)

from datetime import datetime 
from datetime import time 
from datetime import timedelta
s = [
    # package_name time event
    [1, '2017-08-15 13:30:00', 'pickup'], #begin

    [1, '2017-08-15 14:00:00', 'dropoff'], # end (00:30:00)

    [2, '2017-08-15 14:30:00', 'pickup'],  #begin

    [3, '2017-08-15 14:35:00', 'pickup'],

    [2, '2017-08-15 15:00:00', 'dropoff'], 

    [4, '2017-08-15 15:05:00', 'pickup'],

    [3, '2017-08-15 15:10:00', 'dropoff'],

    [4, '2017-08-15 15:40:00', 'dropoff'], #end (01:10:00)

] #total_active_time 1:40:00


datetime_format = '%Y-%m-%d %H:%M:%S'

pckg_inHand = 0
st_time = ''
total_active_time = timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0)

for log in s:
    if log[2] == 'pickup':
        if pckg_inHand == 0: #person state is not_active 
            st_time = log[1] #set active state begin time

        pckg_inHand += 1

    else: #it's a dropoff
        pckg_inHand -= 1
        if pckg_inHand == 0:
            start = datetime.strptime(st_time, datetime_format)
            end = datetime.strptime(log[1], datetime_format)
            total_active_time = total_active_time + (end - start)

print(total_active_time)

答案 2 :(得分:1)

尝试以下代码:

from datetime import datetime

def calculateTime(frm, to):
    p = '%Y-%m-%d %H:%M:%S'
    epoch = datetime(1970, 1, 1)
    frmTime = (datetime.strptime(frm, p) - epoch).total_seconds()
    toTime = (datetime.strptime(to, p) - epoch).total_seconds()
    return toTime - frmTime

def printTime(t, form='H:M'):
    h = int(t/(60*60))
    m = int(60 * (t/(60*60) - h))
    s = int(t - ((h * 60 * 60) + (m * 60)))
    if form == 'H:M':
        print h,'hrs ',m,'mins'
    if form == 'H:M:S':
        print h,'hrs ',m,'mins',s,' secs'
#changed
def totalActiveTime(lst):
    orders = []
    activeTime = 0
    prevTime = None
    for i in lst:
        if i[2] == 'pickup':
            orders.append(i[0])
            if prevTime == None:
                prevTime = i[1]
        elif i[2] == 'dropoff':
            orders.remove(i[0])
            if len(orders) == 0:
                activeTime += calculateTime(prevTime, i[1])
                prevTime = None
    return activeTime
s = [

    [1, '2017-08-15 13:30:00', 'pickup'],

    [1, '2017-08-15 14:00:00', 'dropoff'],

    [2, '2017-08-15 14:30:00', 'pickup'],

    [3, '2017-08-15 14:35:00', 'pickup'],

    [2, '2017-08-15 15:00:00', 'dropoff'],

    [4, '2017-08-15 15:05:00', 'pickup'],

    [3, '2017-08-15 15:10:00', 'dropoff'],

    [4, '2017-08-15 15:40:00', 'dropoff'],

]
printTime(totalActiveTime(s))

输出

1 hrs  40 mins