简体   繁体   English

如果连续的元组共享相同的第一个元素,则将元组列表中的元素添加到新列表

[英]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.每个元组由 label 部分和时间戳组成。 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.现在我的目标是创建一个列表并将每个连续的时间戳添加到子列表中,如果 label 部分相同,append label 到它的最后一个元素。 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.但是,在我的方法中,结果列表似乎错过了最后一个 sublist ,除了它的最后一个元素。 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.注意最后一个子列表中缺少时间戳 59071.43。 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.在子列表的第一个条目ìtertools.groupby中使用带有回调标准的ìtertools.groupby。

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). [37000.0, "PS"]不在数据中(或者看起来如此)。 See my comment.看我的评论。
  • 0.0, 71.43, 142.86 there is a not mentioned float casting of 0 0.0, 71.43, 142.86有一个未提及的float铸造0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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