Reputation: 799
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)
Upvotes: 0
Views: 5925
Reputation: 41
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.
Upvotes: 1
Reputation: 887
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}
Upvotes: 3
Reputation: 12015
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'}}]}
Upvotes: 1