I got two json objects that I need to combine together based on ID and do count and sort operations on it.
Here is the first object comments:
[
{
"userId": 1,
"id": 1,
"title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
"body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
},
{
"userId": 1,
"id": 2,
"title": "qui est esse",
"body": "est rerum tempore vitae\nsequi sint nihil reprehenderit dolor beatae ea dolores neque\nfugiat blanditiis voluptate porro vel nihil molestiae ut reiciendis\nqui aperiam non debitis possimus qui neque nisi nulla"
},
{
"userId": 1,
"id": 3,
"title": "ea molestias quasi exercitationem repellat qui ipsa sit aut",
"body": "et iusto sed quo iure\nvoluptatem occaecati omnis eligendi aut ad\nvoluptatem doloribus vel accusantium quis pariatur\nmolestiae porro eius odio et labore et velit aut"
},
{
"userId": 1,
"id": 4,
"title": "eum et est occaecati",
"body": "ullam et saepe reiciendis voluptatem adipisci\nsit amet autem assumenda provident rerum culpa\nquis hic commodi nesciunt rem tenetur doloremque ipsam iure\nquis sunt voluptatem rerum illo velit"
},
]
This is second json object:
[
{
"postId": 1,
"id": 1,
"name": "id labore ex et quam laborum",
"email": "[email protected]",
"body": "laudantium enim quasi est quidem magnam voluptate ipsam eos\ntempora quo necessitatibus\ndolor quam autem quasi\nreiciendis et nam sapiente accusantium"
},
{
"postId": 1,
"id": 2,
"name": "quo vero reiciendis velit similique earum",
"email": "[email protected]",
"body": "est natus enim nihil est dolore omnis voluptatem numquam\net omnis occaecati quod ullam at\nvoluptatem error expedita pariatur\nnihil sint nostrum voluptatem reiciendis et"
},
{
"postId": 1,
"id": 3,
"name": "odio adipisci rerum aut animi",
"email": "[email protected]",
"body": "quia molestiae reprehenderit quasi aspernatur\naut expedita occaecati aliquam eveniet laudantium\nomnis quibusdam delectus saepe quia accusamus maiores nam est\ncum et ducimus et vero voluptates excepturi deleniti ratione"
},
{
"postId": 1,
"id": 4,
"name": "alias odio sit",
"email": "[email protected]",
"body": "non et atque\noccaecati deserunt quas accusantium unde odit nobis qui voluptatem\nquia voluptas consequuntur itaque dolor\net qui rerum deleniti ut occaecati"
},
{
"postId": 2,
"id": 5,
"name": "et fugit eligendi deleniti quidem qui sint nihil autem",
"email": "[email protected]",
"body": "doloribus at sed quis culpa deserunt consectetur qui praesentium\naccusamus fugiat dicta\nvoluptatem rerum ut voluptate autem\nvoluptatem repellendus aspernatur dolorem in"
},
{
"postId": 2,
"id": 6,
"name": "repellat consequatur praesentium vel minus molestias voluptatum",
"email": "[email protected]",
"body": "maiores sed dolores similique labore et inventore et\nquasi temporibus esse sunt id et\neos voluptatem aliquam\naliquid ratione corporis molestiae mollitia quia et magnam dolor"
},
]
Object one is basically posts with poster details and object two is comments with commenter details.
So expected that object one has one to many relationships with second object. For example one post has many comments. This relationship is based on id
in object one is postId
in object two. The ultimate objective is to count and sort post by number of comments.
I attempt the problem with simple for loops and creating new json object, I managed to combine them together, but I dont know how to count and sort them properly.
in the views:
for i in posts:
if (id==postId):
newobj.append(objtwo[i])
count =1
else:
newobj.append(count)
count=0
Normally I use django ORM to sort this but I dont have access to the database and model of the table. How to count and sort the new object so it can return list of posts with most comments counts and descend to lower comments counts?
CodePudding user response:
Assuming your posts
and comments
data structures are lists, you can use python's defaultdict
to count the comments. Then, use posts.sort(key=...)
to sort your posts based on the collected counts using the key
parameter. Altogether, it could like like this:
import json
from collections import defaultdict
posts = [ ... ]
comments = [ ... ]
# data structure to count the to comments
# automatically initializes to 0
comments_per_post = defaultdict(int)
# iterate through the comments to increase the count for the posts
for comment in comments:
comments_per_post[comment['postId']] = 1
# sort the posts based on the counts collected
posts.sort(key=lambda post: comments_per_post[post['id']], reverse=True)
# print them to verify
print(json.dumps(posts, indent=2))
Note: this sorts the posts
array in-place. If you don't want this, you can use sorted_posts = sorted(posts, key=...
instead.
CodePudding user response:
My answer is very similar to Byted's answer.
I would use Counter
from the built-in collections
to count the number of postIds
in the second object.
Then sort the first object by using these counts from the previous step as a sorting key. Counter object returns 0 if a key is not present in it, so just use it as a lookup as a sorting key. The negative sign ensures a descending order (because sorted()
sorts in ascending order by default).
import json
from collections import Counter
counts = Counter([d['postId'] for d in objtwo])
json.dumps(sorted(objone, key=lambda x: -counts[x['id']]), indent=4)
Intermediate output for this input:
print(counts)
# Counter({1: 4, 2: 2})