0

I have this code :

 [
    {
        "info": {
            "a": 65535,
            "b": 50,
        },
        "test": {
            "m": "10300",
            "id": "2008",
        },
    },
    {
        "info": {
            "a": 65535,
            "b": 50,
        },
        "test": {
            "m": "10300",
            "id": "2008",
        },
    },
    {
        "info": {
            "a": 65535,
            "b": 50,
        },
        "test": {
            "m": "10300",
            "id": "2009",
        },
    },
]

I want to have :

[
    "2008" : [
        {
            "info": {
                "a": 65535,
                "b": 50,
             },
            "test": {
                "m": "10300",
                "id": "2008",
             },
        },
        {
            "info": {
                "a": 65535,
                "b": 50,
            },
            "test": {
                "m": "10300",
                "id": "2008",
            },
       },
    ]
    "2009" : [
        {
            "info": {
                "a": 65535,
                "b": 50,
            },
            "test": {
                "m": "10300",
                "id": "2009",
            },
        },
     ]

The idea is to group by key value based on column id. I'm new in python an have no idea how to do that. Please help me. Thx in advance. If you could provide some ideas will be great.I found some examples but can't solve this problem anyway.

My solution :

value = [i for i in array]
res = sorted(value, key=lambda x: x["id"], reverse=True)
3
  • Please show what you've tried... Commented Aug 14, 2018 at 8:29
  • @I'L'I I edited the question Commented Aug 14, 2018 at 8:31
  • Your desired output is a syntax error. Commented Aug 14, 2018 at 8:31

3 Answers 3

3

I think you want a dictionary where each key is the id and the value is your object.

Try the following. data is your array of data.

new = {}
for item in data:
    item_id = item['test']['id']
    new[item_id] = item

You could also use a dictionary comprehension.

new = {item['test']['id']: item for item in data}
Sign up to request clarification or add additional context in comments.

6 Comments

Is getting only the first value
can you take a look please ?
@George what do you mean?
I get : for each id only one item, for example : ` "2008" : [ { "info": { "a": 65535, "b": 50, }, "test": { "m": "10300", "id": "2008", }, },]`
But I have 2 items for key 2008
|
1

Use itertools.groupby

from itertools import groupby
f = lambda d: d['test']['id']
res = {k:list(v) for k,v in groupby(sorted(l, key=f), f)}
pprint(res)

Output

{'2008': [{'info': {'a': 65535, 'b': 50},
           'test': {'id': '2008', 'm': '10300'}},
          {'info': {'a': 65535, 'b': 50},
           'test': {'id': '2008', 'm': '10300'}}],
 '2009': [{'info': {'a': 65535, 'b': 50},
           'test': {'id': '2009', 'm': '10300'}}]}

Comments

1

Let us suppose

array = [
   {
    "info": {
        "a": 65535,
        "b": 50,
        },
    "test": {
        "m": "10300",
        "id": "2008",
        },
    },
    {
    "info": {
        "a": 65535,
        "b": 50,
        },
    "test": {
        "m": "10300",
        "id": "2008",
        },
    },
    {
    "info": {
        "a": 65535,
        "b": 50,
        },
    "test": {
        "m": "10300",
        "id": "2009",
        },
    },
]

Then you can get the expected result by using

modified_data = {}
for entry in array:
    index = entry['test']['id']
    try:
        modified_data[index].append(entry)
    except KeyError:
        modified_data[index] = [entry]
print modified_data

Now let's analyze your code

value = [i for i in array]

After this value will be equal to array itself, assuming that your array initialization is same as mine.

res = sorted(value, key=lambda x: x["id"], reverse=True)

If the above statement is correct then value will not have 'id' hence it will throw an error.

Also, a list can't have a key-value pair. You have to use a dictionary for that.

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.