Value difference comparison within a list in python
Question: I have a nested list that contains different variables in it. I am trying to
check the difference value between two consecutive items, where if a condition
match, group these items together.

i.e.

    
    
    Item 1 happened on 1-6-2012 1 pm
    Item 2 happened on 1-6-2012 4 pm
    Item 3 happened on 1-6-2012 6 pm
    Item 4 happened on 3-6-2012 5 pm
    Item 5 happened on 5-6-2012 5 pm
    

I want to group the items that have gaps less than 24 Hours. In this case,
Items 1, 2 and 3 belong to a group, Item 4 belong to a group and Item 5 belong
to another group. I tried the following code:

    
    
    Time = []
    All_Traps = []
    Traps = []
    Dic_Traps = defaultdict(list)
    Traps_CSV = csv.reader(open("D:/Users/d774911/Desktop/Telstra Internship/Working files/Traps_Generic_Features.csv"))
    for rows in Traps_CSV:
        All_Traps.append(rows)
    
    All_Traps.sort(key=lambda x: x[9])
    for length in xrange(len(All_Traps)):
            if length == (len(All_Traps) - 1):
                break
            Node_Name_1 = All_Traps[length][2]
            Node_Name_2 = All_Traps[length + 1][2]
            Event_Type_1 = All_Traps[length][5]
            Event_Type_2 = All_Traps[length + 1][5]
            Time_1 = All_Traps[length][9]
            Time_2 = All_Traps[length + 1][9]
            Difference = datetime.strptime(Time_2[0:19], '%Y-%m-%dT%H:%M:%S') - datetime.strptime(Time_1[0:19], '%Y-%m-%dT%H:%M:%S')
            if Node_Name_1 == Node_Name_2 and \
               Event_Type_1 == Event_Type_2 and \
               float(Difference.seconds) / (60*60) < 24:
                 Dic_Traps[length].append(All_Traps[Length])
    

But I am missing some items. Ideas?


Answer: For sorted list you may use
[groupby](https://docs.python.org/2/library/itertools.html#itertools.groupby).
Here is a simplified example (you should convert your date strings to datetime
objects), it should give the main idea:

    
    
    from itertools import groupby
    import datetime
    
    SRC_DATA = [
        (1, datetime.datetime(2015, 06, 20, 1)),
        (2, datetime.datetime(2015, 06, 20, 4)),
        (3, datetime.datetime(2015, 06, 20, 5)),
        (4, datetime.datetime(2015, 06, 21, 1)),
        (5, datetime.datetime(2015, 06, 22, 1)),
        (6, datetime.datetime(2015, 06, 22, 4)),
    ]
    
    for group_date, group in groupby(SRC_DATA, key=lambda X: X[1].date()):
        print "Group {}: {}".format(group_date, list(group))
    

Output:

    
    
    $ python python_groupby.py 
    Group 2015-06-20: [(1, datetime.datetime(2015, 6, 20, 1, 0)), (2, datetime.datetime(2015, 6, 20, 4, 0)), (3, datetime.datetime(2015, 6, 20, 5, 0))]
    Group 2015-06-21: [(4, datetime.datetime(2015, 6, 21, 1, 0))]
    Group 2015-06-22: [(5, datetime.datetime(2015, 6, 22, 1, 0)), (6, datetime.datetime(2015, 6, 22, 4, 0))]
    

