2016-12-30 48 views
0

我正在分析如下例所示的序列中發生的事件。 它顯示了元組的列表,這些元組具有關於數據框中類型和索引的元素。 我想保存所有索引,如果它們屬於相同的類型,只要類型不會順序改變。保存序列中的連續索引

l=[('question', 0), 
    ('response', 1), 
    ('response', 2), 
    ('response', 3), 
    ('response', 4), 
    ('response', 5), 
    ('response', 6), 
    ('response', 7), 
    ('response', 8), 
    ('response', 9), 
    ('response', 10), 
    ('response', 11), 
    ('question', 12), 
    ('response', 13), 
    ('response', 14), 
    ('response', 15), 
    ('question', 16), 
    ('response', 17), 
    ('question', 18), 
    ('response', 19), 
    ('question', 20), 
    ('response', 21), 
    ('question', 22) 
    ] 

期望的輸出:

[('query', 0), 
('response', [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]), 
('query', [12]), 
('response', [13, 14, 15]), 
('query', [16]), 
('response', [17]), 
('query', [18]), 
('response', [19]), 
('query', [20]), 
('response', [21])] 

這是我的溶液。有沒有更好的方法來做到這一點?

def fxn(listitem): 
    newlist = None 
    collected_items = [] 
    current_comm_type = listitem[0][0] 
    for element in listitem: 
     if len(collected_items) == 0: 
      collected_items.append(listitem[0]) 
     elif element[0] == current_comm_type: 
      newlist[1].extend([element[1]]) 
     else: 
      if not newlist: 
       current_comm_type = element[0] 
       newlist = [current_comm_type] 
       newlist.append([element[1]]) 
      else: 
       collected_items.append(tuple(newlist)) 
       current_comm_type = element[0] 
       newlist = [current_comm_type] 
       newlist.append([element[1]]) 
      # collected_items.append(newlist) 
    return collected_items 

fxn(l) 
+2

請務必準確地再現您的壓痕發佈Python代碼時。否則,您會在要求人們閱讀的代碼中引入新的錯誤。 – khelwood

回答

4

下面是與itertools.groupby做到這一點的一種方式和列表理解

from itertools import groupby 

r = [(k, [y for _, y in g]) for k, g in groupby(l, lambda x: x[0])] 
print(r) 
# [('question', [0]), ('response', [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]), ('question', [12]), ('response', [13, 14, 15]), ('question', [16]), ('response', [17]), ('question', [18]), ('response', [19]), ('question', [20]), ('response', [21]), ('question', [22])] 
1

下面是一個解決方案作爲發電機:

def my_fxn(input_list): 
    output = None 
    for key, value in input_list: 
     if output is None or key != output[0]: 
      if output is not None: 
       yield output 
      output = (key, [value]) 
     else: 
      output[1].append(value) 
    yield output