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

3
  • Do you want to group by date? Commented Apr 11, 2019 at 11:45
  • @Rakesh yes, order should be 26.03.2019, 01.04.2019,03.04.2019 Commented Apr 11, 2019 at 11:50
  • myComplex.sort(key=lambda x: x[0][0][::-1]) Commented Apr 11, 2019 at 12:12

2 Answers 2

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) 
Sign up to request clarification or add additional context in comments.

Comments

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.