Arda Dizdaroğlu
Arda Dizdaroğlu

Reputation: 71

Python & Arrays : Search and Add Missing Element in Array

I have a messy 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 Subelements that begins with '03.04.2019'; ['03.04.2019', 'Jack', '7']

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

The Subelements 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 add the missing dates between 01.04.2019 and 05.04.2019 (which are 02.04.2019, 04.04.2019, 05.04.2019 for this list) with sub-subelements like this format;

[['02.04.2019', 'George', '0']]
[['04.04.2019', 'George', '0']]
[['05.04.2019', 'George', '0']]

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']], [['02.04.2019', 'George', '0']], [['04.04.2019', 'George', '0']], [['05.04.2019', 'George', '0']]]

How can I do that? Can you give me a solution for this?

Upvotes: 0

Views: 105

Answers (3)

analphagamma
analphagamma

Reputation: 76

Similarly to what Iain D said you could have a dictionary

my_little_bit_complex_dict = {
    '03.04.2019' : [
                    {'name': 'Jack', 'number': 7}
                    ],
    '26.03.2019' : [
                    {'name': 'Michael', 'number': 7},
                    {'name': 'Smith', 'number': 8}
                    ]
}   

But to be honest you might want to consider using a database if the amount of data is big

CREATE TABLE mytable (
    somedate DATE,
    name VARCHAR(255),
    numberthingy INTEGER
)

Upvotes: 0

Angadeon
Angadeon

Reputation: 1

If your output "must" look like that, then I'd imagine you'll just have to rebuild it manually. If you're open to working alternatives though, you might find it easier to manage as a dictionary:

{date: [[name,number],[name,number]], date2: [[name,number]]}

And though it won't matter much to the dictionary, if it has to stay a list array, consider yyyy.mm.dd for easier sorting.

Upvotes: 0

Iain D
Iain D

Reputation: 507

Rather than using lists, I would suggest entering the data into a pandas DataFrame with a timestamp index and name and value columns and then resample to the desired time period and then complete using ffill.

Upvotes: 1

Related Questions