-1

I have a complex array; every element has subelements and every subelement has sub-subelements. My array is;

myComplex=[[['03.04.2019', 'Jack', '7']], [['26.03.2019', 'Micheal', '5'], ['26.03.2019', 'Smith', '8']], [['01.04.2019', 'Jack', '11'], ['01.04.2019', 'Michelle', '2'], ['01.04.2019', 'George', '9']]]

Let me explain this array;

The Subelement that begins with '03.04.2019'; ['03.04.2019', 'Jack', '7']

The Subelement that begins with '26.03.2019'; ['26.03.2019', 'Micheal', '8'], ['26.03.2019', 'Smith', '5']

The Subelement that begins with '01.04.2019'; ['01.04.2019', 'Jack', '11'], ['01.04.2019', 'Michelle', '2'], ['01.04.2019', 'George', '9']

In myComplex above, as you see, every subelements' first sub-subelement is a date. I want to order these subelements with their dates. So I want the output like this when I enter print(myComplex);

[[['26.03.2019', 'Micheal', '5'], ['26.03.2019', 'Smith', '8']], [['01.04.2019', 'Jack', '11'], ['01.04.2019', 'Michelle', '2'], ['01.04.2019', 'George', '9']], [['03.04.2019', 'Jack', '7']]]

How can I do that? Can you give me a solution for this? I asked a similar question in here but now I have more complex array.

2 Answers2

1

Using collections.defaultdict

Ex:

from collections import defaultdict

myComplex=[[['03.04.2019', 'Jack', '7']], [['26.03.2019', 'Micheal', '5'], ['26.03.2019', 'Smith', '8']], [['01.04.2019', 'Jack', '11'], ['01.04.2019', 'Michelle', '2'], ['01.04.2019', 'George', '9']]]
result = defaultdict(list) 
for i in myComplex:
    for j in i:
        result[j[0]].append(j)

print(result.values())

Output:

[[['03.04.2019', 'Jack', '7']],
 [['26.03.2019', 'Micheal', '5'], ['26.03.2019', 'Smith', '8']],
 [['01.04.2019', 'Jack', '11'],
  ['01.04.2019', 'Michelle', '2'],
  ['01.04.2019', 'George', '9']]]

Using itertools.groupby

Ex:

import datetime        
from itertools import groupby, chain

myComplex=[[['03.04.2019', 'Jack', '7']], [['26.03.2019', 'Micheal', '5'], ['26.03.2019', 'Smith', '8']], [['01.04.2019', 'Jack', '11'], ['01.04.2019', 'Michelle', '2'], ['01.04.2019', 'George', '9']]]
data = chain.from_iterable(myComplex)
result = [list(v) for k, v in groupby(sorted(data, key=lambda x: datetime.datetime.strptime(x[0], "%d.%m.%Y")), lambda x: x[0])]
pprint(result) 
Rakesh
  • 81,458
  • 17
  • 76
  • 113
0

I would create a pandas dataframe out of your array and group it after the date column. This dataframe you then could convert back to a "complex" array.

for reference: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.groupby.html

code snippet:

df.groupby("date").apply(set)
Ivo Leist
  • 408
  • 3
  • 12