python group list by timestamp

Here two sublist of data are there,Please help me on grouping this by timestamp with corresponding data

code i tried:(John Galt helped me on this)

flat = [y for x in L for y in x]    # L is the list 
[x[0] for x in flat] + [x[1] for x in flat]
[x[0] for x in L[0]] + [y[1] for x in L for y in x]

 [[('2017-08-08 10:00:38', 5.0), ('2017-08-08 10:05:38', 8.0), ('2017-08-08 10:10:38', 7.5), ('2017-08-08 10:15:38', 8.3), ('2017-08-08 10:20:38', 5.0), ('2017-08-08 10:25:38', 5.0), ('2017-08-08 10:30:38', 5.0), ('2017-08-08 10:35:38', 5.0), ('2017-08-08 10:40:38', 55.0), ('2017-08-08 10:45:38', 85.0), ('2017-08-08 10:50:38', 55.0), ('2017-08-08 10:55:38', 5.0), ('2017-08-08 11:00:38', 53.0)], 
        [('2017-08-08 10:00:38', 11.2), ('2017-08-08 10:05:38', 10.0), ('2017-08-08 10:10:38', 13.0), ('2017-08-08 10:15:38', 101.0), ('2017-08-08 10:25:38', 10.0), ('2017-08-08 10:30:38', 10.0), ('2017-08-08 10:35:38', 110.0), ('2017-08-08 10:45:38', 100.5), ('2017-08-08 10:50:38', 100.5), ('2017-08-08 10:55:38', 10.05), ('2017-08-08 11:00:38', 10.10)]]

Desired output:

['2017-08-08 10:00:38','5.0','10.0'],['2017-08-08 10:05:38','25.0','30.0'],['2017-08-08 10:15:38','25.0','35.0']

2 answers

  • answered 2017-08-12 11:41 Amol B.

    big_list = [[('2017-08-08 10:00:38', 5.0), ('2017-08-08 10:05:38', 8.0), ('2017-08-08 10:10:38', 7.5), ('2017-08-08 10:15:38', 8.3), ('2017-08-08 10:20:38', 5.0), ('2017-08-08 10:25:38', 5.0), ('2017-08-08 10:30:38', 5.0), ('2017-08-08 10:35:38', 5.0), ('2017-08-08 10:40:38', 55.0), ('2017-08-08 10:45:38', 85.0), ('2017-08-08 10:50:38', 55.0),('2017-08-08 10:55:38', 5.0), ('2017-08-08 11:00:38', 53.0)], [('2017-08-08 10:00:38', 11.2), ('2017-08-08 10:05:38', 10.0), ('2017-08-08 10:10:38', 13.0), ('2017-08-08 10:15:38', 101.0), ('2017-08-08 10:25:38', 10.0), ('2017-08-08 10:30:38', 10.0), ('2017-08-08 10:35:38', 110.0), ('2017-08-08 10:45:38', 100.5), ('2017-08-08 10:50:38', 100.5), ('2017-08-08 10:55:38', 10.05), ('2017-08-08 11:00:38', 10.10)]]
    
    big_dict = {}  
    
    for small_list in big_list:  
        for element in small_list:
            if big_dict.has_key(element[0]):
                big_dict[element[0]].append(element[1])
            else:
                big_dict[element[0]] = list()
                big_dict[element[0]].append(element[1])
    
    final_list = list()
    for dict_item in big_dict.items():
        local_list = [dict_item[0]]
        for list_item in dict_item[1]:
            local_list.append(str(list_item))
    
        final_list.append(local_list)
    print final_list
    

  • answered 2017-08-12 13:54 ThomasGuenet

    Flattening the list was a good idea, I continued from that point and use defaultdict which is a tool worth looking at, look at the result of dictionary d:

    big_list = [[('2017-08-08 10:00:38', 5.0), ('2017-08-08 10:05:38', 8.0), ('2017-08-08 10:10:38', 7.5), ('2017-08-08 10:15:38', 8.3), ('2017-08-08 10:20:38', 5.0), ('2017-08-08 10:25:38', 5.0), ('2017-08-08 10:30:38', 5.0), ('2017-08-08 10:35:38', 5.0), ('2017-08-08 10:40:38', 55.0), ('2017-08-08 10:45:38', 85.0), ('2017-08-08 10:50:38', 55.0),('2017-08-08 10:55:38', 5.0), ('2017-08-08 11:00:38', 53.0)], [('2017-08-08 10:00:38', 11.2), ('2017-08-08 10:05:38', 10.0), ('2017-08-08 10:10:38', 13.0), ('2017-08-08 10:15:38', 101.0), ('2017-08-08 10:25:38', 10.0), ('2017-08-08 10:30:38', 10.0), ('2017-08-08 10:35:38', 110.0), ('2017-08-08 10:45:38', 100.5), ('2017-08-08 10:50:38', 100.5), ('2017-08-08 10:55:38', 10.05), ('2017-08-08 11:00:38', 10.10)]]
    
    flat = [y for x in big_list for y in x]
    
    from collections import defaultdict
    d = defaultdict(list)
    for k, val in flat: # k is the key and val the value of dictionary d
        d[k] += [val]   # the += sign works because each new key have a corresponding 
                        # value automatically initialized to an empty list. 
                        # val is added to that empty list
    
    final_list = [ [k]+[str(val) for val in l] for k,l in d.items() ]
    
    print(final_list)
    print(d)