Python3,嵌套的字典比较(递归?) [英] Python3, nested dict comparison (recursive?)

查看:231
本文介绍了Python3,嵌套的字典比较(递归?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个程序来接受一个.csv文件并为票据关闭数据创建metrics。每张票都有一个或多个时间条目;目标是为打开 - > 关闭和<$ c获取delta $ c> time_start - > time_end 这些不是真正的变量,它们只是为了这个问题的目的。



所以,假设我们有票12345有3个时间条目,如:



票:12345
open:2016-09-26 00:00:00.000关闭:2016-09-27 00:01:00.000
time_start:2016-09-26 00:01:00.000 time_end:2016-09-26 00:02:00.000
ticket:12345
open:2016-09-26 00:00:00.000 close:2016-09-27 00:01:00.000
time_start:2016-09-26 00:01:00.000 time_end:2016-09-26 00:02:00.000
ticket:12345
打开:2016-09-26 00:00:00.000关闭:2016-09-27 00:01:00.000
time_start:2016-09-26 00:01:00.000 time_end:2016-09-27 00: 02:00.000



我希望程序显示一个条目,添加deltas / p>

票:12345
Delta开/关(从开始到结束的总时间):
Delta开始/结束: ($总时间的所有票时间条目加起来)



这是我到目前为止;



.csv示例:

 票证,票证类型,已打开,关闭,时间输入日,开始,结束
737385,软件,2016-09-06 12:48:31.680,2016-09-06 15:41:52.933,2016-09-06 00:00:00.000 ,1900-01-01 15:02:00.417,1900-01-01 15:41:00.417
737318,硬件,2016-09-06 12:20:28.403,2016-09-06 14:35: 58.223,2016-09-06 00:00:00.000,1900-01-01 14:04:00.883,1900-01-01 14:35:00.883
737296,打印/扫描/传真,2016-09- 06 11:37:10.387,2016-09-06 13:33:07.577,2016-09-06 00:00:00.000,1900-01-01 13:29:00.240,1900-01-01 13:33:00.240
737273,软件,2016-09-06 10:54:40.177,2016-09-06 13:28:24.140,2016-09-06 00:00:00.000,1900-01-01 13:17: 00.860,1900-01-01 13:28:00.860
737261,软件,2016-09-06 10:33:09.070,2016-09-06 13:19:41.573,2016-09-06 00:00 :00.000,1900-01-01 13:05:00.113,1900-01-01 13:15:00.113
737238,软件,2016-09-06 09:52:57.090,2016-09-06 14: 42:16.287,2016-09-06 00:00:00.000,1900-01-01 12:01:00.350,1900-01-01 12:04:00.350
737238,软件,2016-09-06 09 :52:57.090,2016-09-06 14:42:16.287,2016-09-06 00:00:00.000,1900-01-01 14:36:00.913,1900-01-01 14:42:00.913
737220,密码,2016-09-06 09:28:16.060,2016-09-06 11:41:16.750,2016-09-06 00:00:00.000,1900-01-01 11:30:00.303, 1900-01-01 11:36:00.303
737197,硬件,2016-09-06 08:50:23.197,2016-09-06 14:02:18.817,2016-09-06 00:00:00.000 ,1900-01-01 13:48:00.530,1900-01-01 14:02:00.530
736964,Internal,2016-09-06 01:02:27.453,2016-09-06 05:46: 00.160,2016-09-06 00:00:00.000,1900-01-01 06:38:00.917,1900-01-01 06:45:00.917

class Time_Entry.py:

 #! / usr / bin / python 
from datetime import *

class Time_Entry:

def __init __(self,ticket_no,time_entry_day,opened,closed,start,end) :
self.ticket_no = ticket_no
self.time_entry_day = time_entry_day
self.opened = opened
self.closed = closed
self.start = datetime.strptime(start ,%Y-%m-%d%H:%M:%S.%f')
self.end = datetime.strptime(end,'%Y-%m-%d%H:% M:%S.%f')
self.total_open_close_delta = 0
self.total_start_end_delta = 0

def open_close_delta(self,topen,tclose):
open_time = datetime.strptime(topen,'%Y-%m-%d%H:%M:%S.%f')
如果tclose!='\\N':
close_time = datetime.strptime(tclose,'%Y-%m-%d%H:%M:%S.%f')
self.total_open_close_delta = close_time - open_time

def start_end_delta (self,tstart,tend):
start_time = datetime.strptime(tstart,'%Y-%m-%d%H:%M:%S.%f')
end_time = datetime。 strptime(tend,'%Y-%m-%d%H:%M:%S.%f')
start_end_delta =(end_time - start_time).seconds
self.total_start_end_delta + = start_end_delta
return(self.total_start_end_delta)

def add_start_end_delta(self,delta):
self.total_start_end_delta + = delta

def display b $ b print('Ticket#:%7.7s Start:%-15s End:%-15s Delta:%-10s'%(self.ticket_no,self.start.time(),self.end.time self.total_start_end_delta))

由metrics.py调用:

 #! / usr / bin / python 

import csv
import pprint
从Time_Entry import *

file ='/ home / jmd9qs / userdrive / metrics。 csv'

#设置CSV,加载一个列表db
reader = csv.DictReader(open(file))
dict_list = []

在读者中:
dict_list.append(line)

def load_tickets(ticket_list):
for i,key in enumerate(ticket_list):
ticket_no = key ['Ticket#']
time_entry_day = key ['Time Entry Day']
opened = key ['Opened']
closed = key ['Closed']
start = key ['Start']
end = key ['End']

time_entry = Time_Entry(ticket_no,time_entry_day,opened,closed,start,end)
time_entry.open_close_delta (打开,关闭)
time_entry.start_end_delta(开始,结束)

用于枚举(ticket_list)中的h,key2:
ticket_no2 = key2 ['Ticket#']
time_entry_day2 = key2 ['Time Entry Day']
opened2 = key2 ['Opened']
closed2 = key2 ['Closed']
start2 = key2 ['Start']
end2 = key2 ['End']
time_entry2 = Time_Entry(ticket_no2,time_entry_day2,opened2,closed2,start2,end2)
$ b如果time_entry.ticket_no == time_entry2.ticket_no和i! = h:
#添加delta并从dict中删除第二个time_entry(不计两次)
time_entry2_delta = time_entry2.start_end_delta(start2,end2)
time_entry.add_start_end_delta(time_entry2_delta)
del dict_list [h]
time_entry.display()

load_tickets(dict_list)


$ b b

这似乎工作正常到目前为止;然而,我得到多票的每张票,而不是一个添加'deltas'。 FYI程序显示输出的方式不同于我的例子,这是故意的。请参阅以下示例:

 票证号:738388开始:15:24:00.313000结束:15:35:00.313000 Delta:2400 
票价:738388起点:16:30:00.593000结束:16:40:00.593000 Delta:1260
票价:738381起点:15:40:00.763000结束:16:04:00.767000 Delta:1440
票证号:738357开始:13:50:00.717000结束:14:10:00.717000 Delta:1200
票证号:738231开始:11:16:00.677000结束:11:21:00.677000 Delta:720
票证号:738203开始:16:15:00.710000结束:16:31:00.710000 Delta:2160
票证号:738203开始:09:57:00.060000结束:10:02:00.060000 Delta:1560
票务编号:738203起点:12:26:00.597000结束:12:31:00.597000 Delta:900
票证号码:738135开始:13:25:00.880000结束:13:50:00.880000 Delta:2040
票证号:738124开始:07:56:00.117000结束:08:31:00.117000 Delta:2100
票证号:738121开始:07:47:00.903000结束:07:52:00.903000 Delta:300
票证号:738115开始:07:15:00.443000结束:07:20:00.443000 Delta:300
票证号:737926开始:06:40:00.813000结束:06:47:00.813000 Delta:420
票证号:737684开始:18:50:00.060000结束:20:10:00.060000增量:13380
票证号:737684开始:13:00:00.560000结束:13:08:00.560000增量:8880
票号:737684开始:08:45:00结束:10:00:00 Delta:9480

请注意,有几张票有多个条目,这是我不想要的。



关于风格,约定等的任何注释也欢迎,因为我试图更多的Pythonic

解决方案

这里的问题是,你实现了你仔细检查同一张票。让我更好地解释一下:

  ticket_list = [111111,111111,666666,777777]#让简化考虑ids只

#我试图保持相同的变量名称
for i,key1在枚举(ticket_list):#outer loop

cnt = 1

for h,key2 in enumerate(ticket_list):#inner loop
if key1 == key2 and i!= h:
print('>> match on i:',i, ' - h:',h)
cnt + = 1

print('Found',key1,cnt,'times')
/ pre>

查看如何重复计算 111111

 >> match on i:0  -  h:1 
找到111111 2次
>> match on i:1 - h:0
找到111111 2次
找到666666 1次
找到777777 1次

这是因为你会匹配 111111 ,当内循环检查第一个位置和外部第二个( i:0,h:1 ),并且当外部在第二位置并且内部在第一位置( i:1,h:0





建议的解决方案



对于您的问题,更好的解决方案是将同一票的条目组合在一起,然后累加您的增量。 groupby 是您的任务的理想选择。这里我自由地重写一些代码:



这里我修改构造函数以接受字典本身。它使传递参数后来不乱。我也删除了添加deltas的方法,以后我们将看到为什么。

  import csv 
import itertools
from datetime import *

class Time_Entry (object):

def __init __(self,entry):
self.ticket_no = entry ['Ticket#']
self.time_entry_day = entry ['Time Entry Day' ]
self.opened = datetime.strptime(entry ['Opened'],'%Y-%m-%d%H:%M:%S.%f')
self.closed = datetime.strptime(entry ['Closed'],'%Y-%m-%d%H:%M:%S.%f')
self.start = datetime.strptime(entry ['Start' ],%Y-%m-%d%H:%M:%S.%f')
self.end = datetime.strptime(entry ['End'],'%Y-%m- %d%H:%M:%S.%f')
self.total_open_close_delta =(self.closed - self.opened).seconds
self.total_start_end_delta =(self.end - self.start ).seconds


def display(self):
print('Ticket#:%7.7s Start:%-15s End:%-15s Delta:%-10s' %(self.ticket_no,self.start.time(),self.end.time(),self.total_start_end_delta))


b $ b

这里我们使用列表推导,最终输出将是 Time_Entry 对象的列表:

  with open('metrics.csv')as ticket_list:
time_entry_list = [time_Entry(line)for line in csv.DictReader(ticket_list)]

print(time_entry_list)
#[< Time_Entry object at 0x101142f60> ;,< Time_Entry object at 0x10114d048>,< Time_Entry object at 0x1011fddd8> ...]

在嵌套循环版本中,您继续在内部循环中重建 Time_Entry ,这意味着对于100个条目,您最终将初始化10000个临时变量!创建一个outside列表允许我们只初始化每个 Time_Entry



可以使用 groupby 为了收集在同一列表中具有相同 ticket_no 的所有对象:

  sorted(time_entry_list,key = lambda x:x.ticket_no)
ticket_grps = itertools.groupby(time_entry_list,key = lambda x: x.ticket_no)

ticket = [(id,[t for t in ticket])for id,tickets in ticket_grps]

ticket 中的最终结果是一个列表元组,票证ID在第一个位置,相关联的 Time_Entry 在最后:

  ('737385',[< Time_Entry object at 0x101142f60>]),
#('737318',[< Time_Entry object at 0x10114d048>]),
#('737238' Time_Entry object at 0x1011fdd68> ;,< Time_Entry object at 0x1011fde80>]),
#...]


$ b b

最后,我们可以迭代所有的票证,并再次使用列表推导,我们可以构建一个只包含delta的列表,以便我们可以将它们汇总在一起。你可以看到为什么我们删除旧的方法来更新deltas,因为现在我们只是存储他们的值为单个条目,然后在外部总和。



这里是你的结果:

 票证:
print('ticket:',ticket [0])
#提取deltas列表,然后sum
print('Delta open / close:',sum([entry.total_open_close_delta for ticket in ticket [1]]))
print ',sum([entry.total_start_end_delta for ticket in ticket [1]])
print('(found {} occurrences)'。format(len(ticket [1])))
print )

输出:

 票:736964 
三角洲开放/关闭:17012
三角洲开始/结束:420
(找到1次)

票:737197
Delta开/关:18715
三角洲开始/结束:840
(找到1次)

票:737220
三角洲开启/关闭:7980
三角洲开始/结束:360
(找到1次)

票:737238
三角洲开放/关闭:34718
三角洲开始/结束:540
(找到2次)

票证:737261
三角洲开放/关闭:9992
三角洲开始/结束:600
(找到1次)
$ b票:737273
Delta开/关:9223
三角洲开始/结束:660
(找到1次)

票:737296
Delta开/关:6957
三角洲开始/结束:240
(找到1次)

票:737318
三角洲开启/关闭:8129
Delta开始/结束:1860
(找到1次)

票:737385
Delta开/关:10401
Delta开始/结束:2340
(找到1次出现)

故事结束时:有用的,它们允许你使用超紧凑的语法做很多东西。此外,python标准库包含很多可以真正帮助你的辅助工具,所以熟悉!


I'm writing a program to take a .csv file and create 'metrics' for ticket closure data. Each ticket has one or more time entries; the goal is to grab the 'delta' (ie - time difference) for open -> close and time_start -> time_end on a PER TICKET basis; these are not real variables, they're just for the purpose of this question.

So, say we have ticket 12345 that has 3 time entries like so:

ticket: 12345 open: 2016-09-26 00:00:00.000 close: 2016-09-27 00:01:00.000 time_start: 2016-09-26 00:01:00.000 time_end: 2016-09-26 00:02:00.000 ticket: 12345 open: 2016-09-26 00:00:00.000 close: 2016-09-27 00:01:00.000 time_start: 2016-09-26 00:01:00.000 time_end: 2016-09-26 00:02:00.000 ticket: 12345 open: 2016-09-26 00:00:00.000 close: 2016-09-27 00:01:00.000 time_start: 2016-09-26 00:01:00.000 time_end: 2016-09-27 00:02:00.000

I'd like to have the program display ONE entry for this, adding up the 'deltas', like so:

ticket: 12345 Delta open/close ($total time from open to close): Delta start/end: ($total time of ALL ticket time entries added up)

Here's what I have so far;

.csv example:

Ticket #,Ticket Type,Opened,Closed,Time Entry Day,Start,End
737385,Software,2016-09-06 12:48:31.680,2016-09-06 15:41:52.933,2016-09-06 00:00:00.000,1900-01-01 15:02:00.417,1900-01-01 15:41:00.417
737318,Hardware,2016-09-06 12:20:28.403,2016-09-06 14:35:58.223,2016-09-06 00:00:00.000,1900-01-01 14:04:00.883,1900-01-01 14:35:00.883
737296,Printing/Scan/Fax,2016-09-06 11:37:10.387,2016-09-06 13:33:07.577,2016-09-06 00:00:00.000,1900-01-01 13:29:00.240,1900-01-01 13:33:00.240
737273,Software,2016-09-06 10:54:40.177,2016-09-06 13:28:24.140,2016-09-06 00:00:00.000,1900-01-01 13:17:00.860,1900-01-01 13:28:00.860
737261,Software,2016-09-06 10:33:09.070,2016-09-06 13:19:41.573,2016-09-06 00:00:00.000,1900-01-01 13:05:00.113,1900-01-01 13:15:00.113
737238,Software,2016-09-06 09:52:57.090,2016-09-06 14:42:16.287,2016-09-06 00:00:00.000,1900-01-01 12:01:00.350,1900-01-01 12:04:00.350
737238,Software,2016-09-06 09:52:57.090,2016-09-06 14:42:16.287,2016-09-06 00:00:00.000,1900-01-01 14:36:00.913,1900-01-01 14:42:00.913
737220,Password,2016-09-06 09:28:16.060,2016-09-06 11:41:16.750,2016-09-06 00:00:00.000,1900-01-01 11:30:00.303,1900-01-01 11:36:00.303
737197,Hardware,2016-09-06 08:50:23.197,2016-09-06 14:02:18.817,2016-09-06 00:00:00.000,1900-01-01 13:48:00.530,1900-01-01 14:02:00.530
736964,Internal,2016-09-06 01:02:27.453,2016-09-06 05:46:00.160,2016-09-06 00:00:00.000,1900-01-01 06:38:00.917,1900-01-01 06:45:00.917

class Time_Entry.py:

#! /usr/bin/python
from datetime import *

class Time_Entry:

def __init__(self, ticket_no, time_entry_day, opened, closed, start, end):
    self.ticket_no = ticket_no
    self.time_entry_day = time_entry_day
    self.opened = opened
    self.closed = closed
    self.start = datetime.strptime(start, '%Y-%m-%d %H:%M:%S.%f')
    self.end = datetime.strptime(end, '%Y-%m-%d %H:%M:%S.%f')
    self.total_open_close_delta = 0
    self.total_start_end_delta = 0

def open_close_delta(self, topen, tclose):
    open_time = datetime.strptime(topen, '%Y-%m-%d %H:%M:%S.%f')
    if tclose != '\\N':
        close_time = datetime.strptime(tclose, '%Y-%m-%d %H:%M:%S.%f')
        self.total_open_close_delta = close_time - open_time

def start_end_delta(self, tstart, tend):
    start_time = datetime.strptime(tstart, '%Y-%m-%d %H:%M:%S.%f')
    end_time = datetime.strptime(tend, '%Y-%m-%d %H:%M:%S.%f')
    start_end_delta = (end_time - start_time).seconds
    self.total_start_end_delta += start_end_delta
    return (self.total_start_end_delta)

def add_start_end_delta(self, delta):
    self.total_start_end_delta += delta

def display(self):
    print('Ticket #: %7.7s Start: %-15s End: %-15s Delta: %-10s' % (self.ticket_no, self.start.time(), self.end.time(), self.total_start_end_delta))

Which is called by metrics.py:

#! /usr/bin/python

import csv
import pprint
from Time_Entry import *

file = '/home/jmd9qs/userdrive/metrics.csv'

# setup CSV, load up a list of dicts
reader = csv.DictReader(open(file))
dict_list = []

for line in reader:
    dict_list.append(line)

def load_tickets(ticket_list):
    for i, key in enumerate(ticket_list):
        ticket_no = key['Ticket #']
        time_entry_day = key['Time Entry Day']
        opened = key['Opened']
        closed = key['Closed']
        start = key['Start']
        end = key['End']

        time_entry = Time_Entry(ticket_no, time_entry_day, opened, closed, start, end)
        time_entry.open_close_delta(opened, closed)
        time_entry.start_end_delta(start, end)

        for h, key2 in enumerate(ticket_list):
            ticket_no2 = key2['Ticket #']
            time_entry_day2 = key2['Time Entry Day']
            opened2 = key2['Opened']
            closed2 = key2['Closed']
            start2 = key2['Start']
            end2 = key2['End']
            time_entry2 = Time_Entry(ticket_no2, time_entry_day2, opened2, closed2, start2, end2)

            if time_entry.ticket_no == time_entry2.ticket_no and i != h:
                # add delta and remove second time_entry from dict (no counting twice)
                time_entry2_delta = time_entry2.start_end_delta(start2, end2)
                time_entry.add_start_end_delta(time_entry2_delta)
                del dict_list[h]
    time_entry.display()

load_tickets(dict_list)

This seems to work OK so far; however, I get multiple lines of output per ticket instead of one with the 'deltas' added. FYI the way the program displays output is different from my example, which is intentional. See example below:

Ticket #:  738388 Start: 15:24:00.313000 End: 15:35:00.313000 Delta: 2400      
Ticket #:  738388 Start: 16:30:00.593000 End: 16:40:00.593000 Delta: 1260      
Ticket #:  738381 Start: 15:40:00.763000 End: 16:04:00.767000 Delta: 1440      
Ticket #:  738357 Start: 13:50:00.717000 End: 14:10:00.717000 Delta: 1200      
Ticket #:  738231 Start: 11:16:00.677000 End: 11:21:00.677000 Delta: 720       
Ticket #:  738203 Start: 16:15:00.710000 End: 16:31:00.710000 Delta: 2160      
Ticket #:  738203 Start: 09:57:00.060000 End: 10:02:00.060000 Delta: 1560      
Ticket #:  738203 Start: 12:26:00.597000 End: 12:31:00.597000 Delta: 900       
Ticket #:  738135 Start: 13:25:00.880000 End: 13:50:00.880000 Delta: 2040      
Ticket #:  738124 Start: 07:56:00.117000 End: 08:31:00.117000 Delta: 2100      
Ticket #:  738121 Start: 07:47:00.903000 End: 07:52:00.903000 Delta: 300       
Ticket #:  738115 Start: 07:15:00.443000 End: 07:20:00.443000 Delta: 300       
Ticket #:  737926 Start: 06:40:00.813000 End: 06:47:00.813000 Delta: 420       
Ticket #:  737684 Start: 18:50:00.060000 End: 20:10:00.060000 Delta: 13380     
Ticket #:  737684 Start: 13:00:00.560000 End: 13:08:00.560000 Delta: 8880      
Ticket #:  737684 Start: 08:45:00        End: 10:00:00        Delta: 9480      

Note that there are a few tickets with more than one entry, which is what I don't want.

Any notes on style, convention, etc. are also welcome as I'm trying to be more 'Pythonic'

解决方案

The problem here is that with a nested loop like the one you implemented you double-examine the same ticket. Let me explain it better:

ticket_list = [111111, 111111, 666666, 777777] # lets simplify considering the ids only

# I'm trying to keep the same variable names
for i, key1 in enumerate(ticket_list): # outer loop

    cnt = 1

    for h, key2 in enumerate(ticket_list): # inner loop
        if key1 == key2 and i != h:
            print('>> match on i:', i, '- h:', h)
            cnt += 1

    print('Found', key1, cnt, 'times')

See how it double counts the 111111

>> match on i: 0 - h: 1
Found 111111 2 times
>> match on i: 1 - h: 0
Found 111111 2 times
Found 666666 1 times
Found 777777 1 times

That's because you will match the 111111 both when the inner loop examines the first position and the outer the second (i: 0, h: 1), and again when the outer is on the second position and the inner is on the first (i: 1, h: 0).


A proposed solution

A better solution for your problem is to group the entries of the same ticket together and then sum your deltas. groupby is ideal for your task. Here I took the liberty to rewrite some code:

Here I modified the constructor in order to accept the dictionary itself. It makes passing the parameters later less messy. I also removed the methods to add the deltas, later we'll see why.

import csv
import itertools
from datetime import *

class Time_Entry(object):

    def __init__(self, entry):
        self.ticket_no = entry['Ticket #']
        self.time_entry_day = entry['Time Entry Day']
        self.opened = datetime.strptime(entry['Opened'], '%Y-%m-%d %H:%M:%S.%f')
        self.closed = datetime.strptime(entry['Closed'], '%Y-%m-%d %H:%M:%S.%f')
        self.start = datetime.strptime(entry['Start'], '%Y-%m-%d %H:%M:%S.%f')
        self.end = datetime.strptime(entry['End'], '%Y-%m-%d %H:%M:%S.%f')
        self.total_open_close_delta = (self.closed - self.opened).seconds
        self.total_start_end_delta = (self.end - self.start).seconds


    def display(self):
        print('Ticket #: %7.7s Start: %-15s End: %-15s Delta: %-10s' % (self.ticket_no, self.start.time(), self.end.time(), self.total_start_end_delta))

Here we load the data using list comprehensions, the final output will be a the list of Time_Entry objects:

with open('metrics.csv') as ticket_list:
    time_entry_list = [Time_Entry(line) for line in csv.DictReader(ticket_list)]

print(time_entry_list)
# [<Time_Entry object at 0x101142f60>, <Time_Entry object at 0x10114d048>, <Time_Entry object at 0x1011fddd8>, ... ]

In the nested-loop version instead you kept rebuilding the Time_Entry inside the inner loop, which means for 100 entries you end up initializing 10000 temporary variables! Creating a list "outside" instead allows us to initialize each Time_Entry only once.

Here comes the magic: we can use the groupby in order to collect all the objects with the same ticket_no in the same list:

sorted(time_entry_list, key=lambda x: x.ticket_no)
ticket_grps = itertools.groupby(time_entry_list, key=lambda x: x.ticket_no)

tickets = [(id, [t for t in tickets]) for id, tickets in ticket_grps]

The final result in ticket is a list tuples with the ticket id in the first position, and the list of associated Time_Entry in the last:

print(tickets)
# [('737385', [<Time_Entry object at 0x101142f60>]),
#  ('737318', [<Time_Entry object at 0x10114d048>]),
#  ('737238', [<Time_Entry object at 0x1011fdd68>, <Time_Entry object at 0x1011fde80>]),
#  ...]

So finally we can iterate over all the tickets, and using again a list comprehension we can build a list containing only the deltas so we can sum them together. You can see why we removed the old method to update the deltas, since now we simply store their value for the single entry and then sum them externally.

Here is your result:

for ticket in tickets:
    print('ticket:', ticket[0])
    # extract list of deltas and then sum
    print('Delta open / close:', sum([entry.total_open_close_delta for entry in ticket[1]]))
    print('Delta start / end:', sum([entry.total_start_end_delta for entry in ticket[1]]))
    print('(found {} occurrences)'.format(len(ticket[1])))
    print()

Output:

ticket: 736964
Delta open / close: 17012
Delta start / end: 420
(found 1 occurrences)

ticket: 737197
Delta open / close: 18715
Delta start / end: 840
(found 1 occurrences)

ticket: 737220
Delta open / close: 7980
Delta start / end: 360
(found 1 occurrences)

ticket: 737238
Delta open / close: 34718
Delta start / end: 540
(found 2 occurrences)

ticket: 737261
Delta open / close: 9992
Delta start / end: 600
(found 1 occurrences)

ticket: 737273
Delta open / close: 9223
Delta start / end: 660
(found 1 occurrences)

ticket: 737296
Delta open / close: 6957
Delta start / end: 240
(found 1 occurrences)

ticket: 737318
Delta open / close: 8129
Delta start / end: 1860
(found 1 occurrences)

ticket: 737385
Delta open / close: 10401
Delta start / end: 2340
(found 1 occurrences)

At the end of the story: list comprehensions can be super-useful, they allows you to do a lot of stuff with a super-compact syntax. Also the python standard library contains a lot of ready-to-use tools that can really come to your aid, so get familiar!

这篇关于Python3,嵌套的字典比较(递归?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆