Python 如何迭代每个组的表并存储列表以进行顺序分析?

Python 如何迭代每个组的表并存储列表以进行顺序分析?,python,sequential,Python,Sequential,我需要程序返回一个列表列表,其中的列表是每个人的活动,即按人分组-从一个列person和activity的表开始 例如,对列['1'、'1'、'2']和活动列['a'、'b'、'a']的测试应该返回['a'、'b']、['a']],因为人“1”有活动“a”和“b”,而人“2”有活动“a” 目的是分析活动的顺序或流程 我尝试了以下方法: #having a table with columns person, activity and day, stored in lists: person=['

我需要程序返回一个列表列表,其中的列表是每个人的活动,即按人分组-从一个列person和activity的表开始

例如,对列['1'、'1'、'2']和活动列['a'、'b'、'a']的测试应该返回['a'、'b']、['a']],因为人“1”有活动“a”和“b”,而人“2”有活动“a”

目的是分析活动的顺序或流程

我尝试了以下方法:

#having a table with columns person, activity and day, stored in lists:
person=['1','2','2','2','3','3']
activity=['a','b','c','d','b','c']

#starting with an empty list to store the lists
list_of_lists=[]

#starting with an empty current list
current_list=[]

#iterating each row
for i in range(len(person)):

#if the list of list is empty (in the beginning)
    if len(list_of_lists)==0:

#append the i'th activity in current list
        current_list.append(activity[i])

#otherwise if the i'th person is the same as the latter, append to the same list
    elif person[i]==person[i-1]:
        current_list.append(activity[i])

#otherwise (if it iterates over a a new person) store the current list and create a new list
    else:
        list_of_lists.append(current_list)
        current_list=list()

列表中的列表开始为空;因为在这种情况下它不会被改变,所以它永远不会被改变,这就是为什么它最后是空的。(没有任何内容被“覆盖”)。

在可重用项之间进行分组的有效方法是使用


正如@Scott Hunter所说的,
list of_list
开始时是空的,并保持为空。代码的另一个问题是确保最终的
当前\u列表
添加到
列表中。您可以这样调整代码:

persons = ['1','2','2','2','3','3']
activities = ['a','b','c','d','b','c']
list_of_lists = []
current_list = []
for i in range(len(persons)):
    if i==0:  # use i == 0 instead of len(list_of_lists)==0 as the starting condition
        current_list.append(activities[i])
    elif persons[i]==persons[i-1]:
        current_list.append(activities[i])
    else:
        list_of_lists.append(current_list)
        current_list=[activities[i]]   # remember to add the current activity here
if current_list:     # after loop has finished still need to add the current list
    list_of_lists.append(current_list)
使用
zip
person
列表的“滞后”版本,这可以简化很多

result = []
for person, previous, activity in zip(persons, [None] + persons, activities):
    if person == previous:
        result[-1].append(activity)
    else:
        result.append([activity])
输出:

[['a'], ['b', 'c', 'd'], ['b', 'c']]

当您使用调试器跟踪这一点时,第一点是什么,事情的表现与您预期的不同?谢谢,这两个版本都做了很好的阐述!“[None]+persons”是如何工作的?通过在列表的开头添加
None
,您可以将所有其他人向右移动一个位置。将转换后的版本压缩到列表的原始版本可以让您将每个人与前一个人进行比较。谢谢!我将仔细看看这个工具包。在这种情况下,下划线(“for””)起什么作用?您将如何迭代其他列,例如日期列?
只是变量的名称/标识符,就像
一样。它通常用作变量名,表示捕获的变量不重要-
itertools。groupby
生成一个键和组,在这种情况下,我只关心组。迭代一个额外的日期列的期望输出是什么?它是否会简单地拆分为与拆分活动子列表长度相同的子列表?感谢您的澄清。对附加日期列进行迭代的理想输出将是使用时间维度丰富流/顺序分析。我最初的想法是以不同的方式构造数据,并使用2D数组(其中每个列表都是一行)-有什么想法吗?我的意思是,如果你能控制数据的外观,我可能会使用字典。我的意思是,一些示例输出会是什么样子?谢谢。我将仔细研究这些工具。itemgetter很有趣。
from itertools import groupby, islice


people = ["1", "2", "2", "2", "3", "3"]
activities = ["a", "b", "c", "d", "b", "c"]

activities_iter = iter(activities)

activity_groups = [list(islice(activities_iter, len(list(group)))) for _, group in groupby(people)]
print(activity_groups)
[['a'], ['b', 'c', 'd'], ['b', 'c']]