Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 按顺序保存连续索引_Python_Itertools - Fatal编程技术网

Python 按顺序保存连续索引

Python 按顺序保存连续索引,python,itertools,Python,Itertools,我正在分析按如下示例的顺序发生的事件。 它显示了一个元组列表,其中包含关于数据帧中的类型和索引的元素。 我想保存所有索引,如果它们属于同一类型,只要类型不按顺序更改 l=[('question', 0), ('response', 1), ('response', 2), ('response', 3), ('response', 4), ('response', 5), ('response', 6), ('response', 7), ('re

我正在分析按如下示例的顺序发生的事件。 它显示了一个元组列表,其中包含关于数据帧中的类型和索引的元素。 我想保存所有索引,如果它们属于同一类型,只要类型不按顺序更改

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)

这里有一种方法可以通过阅读和理解列表来完成:


这里有一种方法可以通过阅读和理解列表来完成:


以下是作为生成器的解决方案:

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

以下是作为生成器的解决方案:

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

在发布Python代码时,请确保准确地再现缩进。否则,您将在要求人们阅读的代码中引入新的错误。请确保在发布Python代码时准确地再现缩进。否则,您将在要求人们阅读的代码中引入新的错误。