从列表列表到列表列表列表

From list of lists to list of lists of lists

我有一个这样的列表:

first_list = [[ 1.        , 45.4,  9.1],
              [ 2.        , 45.5,  9.1],
              [ 2.        , 45.4,  9.2],
              [ 2.        , 45.4,  9.2],
              [ 3.        , 45.4,  9.1],
              [ 3.        , 45.4,  9.1],
              [ 3.        , 45.4,  9.1] ]

我想使用 folio 函数 HeatMapWithTime,为此我需要根据每个子列表 (1., 2., 3. ecc) 的第一项对上面的数据进行分组:

new_list = [ [ [45.4, 9.1] ],                               # All coords for 1.
             [ [45.5, 9.1], [45.4, 9.2], [45.4, 9.2] ],     # All coords for 2.
             [ [45.4, 9.1], [45.4, 9.1], [45.4, 9.2] ] ]    # All coords for 3.

我该怎么做?

假设列表按第一个元素排序,看起来,您可以使用 itertools.groupby:

from itertools import groupby
from operator import itemgetter
[[i[1:] for i in v] for k,v in groupby(first_list, itemgetter(0))]

#[[[45.4, 9.1]],
# [[45.5, 9.1], [45.4, 9.2], [45.4, 9.2]],
# [[45.4, 9.1], [45.4, 9.1], [45.4, 9.1]]]

一种方法是先对列表进行排序:

lst_data = sorted(first_list)

然后遍历它,当拳头索引改变时创建一个新的 ljst:

first_index = None
final_lst = []
for i in lst_data:
    if i[0] != first_index:
        final_lst.append([])
        first_index = i[0]
    final_lst[-1].append(i[1:])

我会为此使用字典,如果您需要它作为列表,您可能希望将其带回列表,但使用字典进行分组通常很有帮助:

first_list = [[ 1.        , 45.4,  9.1],
              [ 2.        , 45.5,  9.1],
              [ 2.        , 45.4,  9.2],
              [ 2.        , 45.4,  9.2],
              [ 3.        , 45.4,  9.1],
              [ 3.        , 45.4,  9.1],
              [ 3.        , 45.4,  9.1] ]
result = dict()
for group, *values in first_list:
    if group not in result:
        result[group] = [values]
    else:
        result[group].append(values)
print(result)
### if you want it back as a list:
result_list = [v for k,v in result.items()]
print(result_list)

输出:

#dict:
{1.0: [[45.4, 9.1]], 2.0: [[45.5, 9.1], [45.4, 9.2], [45.4, 9.2]], 3.0: [[45.4, 9.1], [45.4, 9.1], [45.4, 9.1]]}
#list:
[[[45.4, 9.1]], [[45.5, 9.1], [45.4, 9.2], [45.4, 9.2]], [[45.4, 9.1], [45.4, 9.1], [45.4, 9.1]]]

您可以将所有坐标收集到字典中:

res = {}
for entry in first_list:
    res.setdefault(entry[0], []).append(entry[1:])

这给你:

>>> res
{1.0: [[45.4, 9.1]],
 2.0: [[45.5, 9.1], [45.4, 9.2], [45.4, 9.2]],
 3.0: [[45.4, 9.1], [45.4, 9.1], [45.4, 9.1]]}

如果您的列表已经排序,请将值转换为列表(仅限 Python 3.6+):

>>> list(res.values())
[[[45.4, 9.1]],
 [[45.5, 9.1], [45.4, 9.2], [45.4, 9.2]],
 [[45.4, 9.1], [45.4, 9.1], [45.4, 9.1]]]

否则,您需要先对它们进行排序:

>>> [res[key] for key in sorted(res.keys())]
[[[45.4, 9.1]],
 [[45.5, 9.1], [45.4, 9.2], [45.4, 9.2]],
 [[45.4, 9.1], [45.4, 9.1], [45.4, 9.1]]]

使用pandas的一种解决方案,处理复杂数据格式时的明智选择:

import pandas as pd    
pd.DataFrame(first_list).set_index(0).groupby(df.index).apply(lambda x: x.values.tolist()).tolist()
#-> 
[[[45.4, 9.1]],
 [[45.5, 9.1], [45.4, 9.2], [45.4, 9.2]],
 [[45.4, 9.1], [45.4, 9.1], [45.4, 9.1]]]