python 如何使用JSON对象的计数对其进行合并和排序?

2jcobegt  于 2022-11-21  发布在  Python
关注(0)|答案(2)|浏览(355)

我有两个json对象,需要根据ID将它们合并在一起,并对其进行计数和排序操作。
下面是第一个对象注解:

[
      {
        "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"
      },
    ]

这是第二个json对象:

[
  {
    "postId": 1,
    "id": 1,
    "name": "id labore ex et quam laborum",
    "email": "Eliseo@gardner.biz",
    "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": "Jayne_Kuhic@sydney.com",
    "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": "Nikita@garfield.biz",
    "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": "Lew@alysha.tv",
    "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": "Presley.Mueller@myrl.com",
    "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": "Dallas@ole.me",
    "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"
  },

]

对象一基本上是帖子与海报的详细信息,对象二是评论与评论者的详细信息。
因此,期望对象1与第二个对象具有一对多关系。例如,一个帖子具有许多评论。此关系基于对象1中的id是对象2中的postId。最终目标是计数按评论数排序帖子。
我尝试用简单的for循环和创建新的json对象来解决这个问题,我设法将它们组合在一起,但我不知道如何正确地计数和排序它们。
在视图中:

for i in posts:
  if (id==postId):
    newobj.append(objtwo[i])
    count+=1
  else:
    newobj.append(count)
    count=0

通常我使用django ORM来排序,但是我没有访问数据库和表格模型的权限。如何对新对象进行计数和排序,以便它可以返回评论数最多的帖子列表,并降低评论数?

68bkxrlz

68bkxrlz1#

假设你的postscomments数据结构都是列表,你可以使用python的defaultdict来计算评论数,然后使用posts.sort(key=...),根据key参数收集到的评论数对你的帖子进行排序,总的来说,它可以像这样:

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))

注意:这会对posts数组进行就地排序。如果你不想这样,你可以使用sorted_posts = sorted(posts, key=...

e4eetjau

e4eetjau2#

我的答案和字节跳动的答案非常相似。
我将使用内置collections中的Counter来计算第二个对象中postIds的个数。
然后使用上一步中的计数作为排序关键字对第一个对象进行排序。如果计数器对象中没有关键字,则返回0,因此只需将其作为排序关键字进行查找。负号确保降序排序(因为sorted()默认按升序排序)。

import json
from collections import Counter

# count the comments
counts = Counter([d['postId'] for d in objtwo])

# add the counts to each post
for d in objone:
    d["number of comments"] = counts[d['id']]

# sort posts by number of comments in descending order
objone.sort(key=lambda x: -x['number of comments'])

# convert to json
json.dumps(objone, indent=4)

此输入的中间输出:

print(counts)
# Counter({1: 4, 2: 2})

相关问题