简体   繁体   中英

Add elements from list of tuples to new list if consecutive tuples share the same first element

I have a list of tuples which looks something like this:

[['PS', 0], ['PS', 71.43], ['PS', 142.86],  ['tut', 37071.43],  ['tut', 59000.0], ['PS', 59071.43], ['PS', 61500.0]]

Each tuple consists of a section label and a timestamp. Now my goal is to make a list and add each consecutive timestamp to a sublist if the section label is the same and append the label to the last element of it. So the list would look something like this:

[[0.0, 71.43, 142.86, [37000.0, "PS"]], [37071.43, [59000.0, "tut"]], [59071.43, [61500.0, "PS"]]]

However, in my approach the resulting list seems to miss the last sublist , apart from the very last element of it. So it ends up looking like this:

[['PS', 0], ['PS', 71.43], ['PS', 142.86],  ['tut', 37071.43],  ['tut', 59000.0], ['PS', 61500.0]]

Notice how the timestamp 59071.43 is missing in the last sublist. My current approach looks like this:

def same_sec(tuple_list):
    one_tuple = [] #sublist
    sec_list= [] #list of sublists
    print(len(tuple_list))
    for i in range(len(tuple_list)):
        #assuming last element always belongs to the last group of section ids
        if i == len(tuple_list) - 1: #add the last element
            sec_list[-1].append([tuple_list[i][0],tuple_list[i][1]])
         #if two consecutive labels are the same add it to sublist   
        elif (tuple_list[i][0] == tuple_list[i+1][0]): 
            one_tuple.append(tuple_list[i][1])
        #if next label is different add current element to sublist and add sublist to list
        else: 
            one_tuple.append([tuple_list[i][0],tuple_list[i][1]])
            sec_list.append(one_tuple)
            one_tuple = []  #reset sublist  
    return sec_list

Any help is greatly appreciated!

Use ìtertools.groupby with a callback criteria wrt the 1st entry, the label, of the sublist.

from itertools import groupby


lst = [['PS', 0], ['PS', 71.43], ['PS', 142.86],  ['tut', 37071.43],  ['tut', 59000.0], ['PS', 59071.43], ['PS', 61500.0]]

out = []
for grp_id, grp in groupby(lst, key=lambda pair: pair[0]):
    tmp = [v for _, v in grp]
    out.append([tmp + [[tmp.pop(), grp_id]]])

print(out)
[[[0, 71.43, [142.86, 'PS']]], [[37071.43, [59000.0, 'tut']]], [[59071.43, [61500.0, 'PS']]]]

Notice that:

  • [37000.0, "PS"] is not in the data (or it seems so). See my comment.
  • 0.0, 71.43, 142.86 there is a not mentioned float casting of 0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM