简体   繁体   中英

From list of lists to list of lists of lists

i have a list like this:

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] ]

I want to use the folio function HeatMapWithTime , and to do that i need to group the data above according to the first item of each sublist (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.

How can i do that?

Assuming the list is sorted by the first elements, as it seems, you can use 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]]]

You can collect all coordinates in a dictionary:

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

This gives you:

>>> 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]]}

If your list was already sorted, convert the values into a list (Python 3.6+ only):

>>> 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]]]

Otherwise, you need to sort them first:

>>> [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]]]

One way to do this is to first sort your list:

lst_data = sorted(first_list)

And then to loop over it, creating a new ljst when the fist index changes:

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:])

I would use a dict for that, you might want to bring it back to a list, if you need it as a list, but using a dict for grouping is usually helpful:

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)

Output:

#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]]]

One solution using pandas , a wise choice when dealing with complex data formats:

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]]]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM