Мне нужна программа, чтобы возвратить список списков, в которых списки являются действиями каждого человека, то есть сгруппированы по лицам - начиная с таблицы столбцов персонажа и действия.
Например, проверка столбца ['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()