Convert tuples to list of dictionary

I'm having trouble converting two tuples into a list of dictionaries. Here is the structure:

train_detail = (Counter({2: 50, 0: 62, 1: 38}),
 {2: 0.3333333333333333, 0: 0.41333333333333333, 1: 0.25333333333333335})

test_detail = (Counter({2: 6, 0: 49, 1: 4}),
 {2: 0.1016949152542373, 0: 0.8305084745762712, 1: 0.06779661016949153})

Now i want to turn these two into a structure like the following:

 [
        {
            "label": "0",
            "trainPercent": 0.41333333333333333,
            "trainNumber": 62,
            "testPercent": 0.8305084745762712,
            "testNumber": 49,
        },
        {
            "label": "1",
            "trainPercent": 0.25333333333333335,
            "trainNumber": 38,
            "testPercent": 0.06779661016949153,
            "testNumber": 4,
        },
        {
            "label": "2",
            "trainPercent": 0.3333333333333333,
            "trainNumber": 50,
            "testPercent": 0.1016949152542373,
            "testNumber": 6,
        },      
    ]

What's an effective way of doing that with minimum looping? Thank you. Note Counter is a subclass of dict so inherited every methods of a regular dict.



Read more here: https://stackoverflow.com/questions/64400097/convert-tuples-to-list-of-dictionary

Content Attribution

This content was originally published by Yehui He at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: