如何使用Python获取JSON文件中各个类别的百分位数?

huangapple go评论67阅读模式
英文:

How to get the percentile of categories in a JSON file using Python?

问题

如何计算位于Python列表中的类别的百分位数

我有一个巨大的JSON要处理,假设我是Python的新手(我有编程背景),我该如何获得json中包含的每个类别的百分位数?

我试图至少获取每个类别,但我真的不认为我编写的代码是高效的。

def countCategory(file):
    categorieslist = list()
    for book in file:
        categories = book["categories"]
        for category in categories:
            if category not in categorieslist:
                categorieslist.append(category)
    return categorieslist
英文:

How to count percentiles of categories located in an list in Python

I have this giant JSON to work with, assuming I'm new to Python (I do have background in programming), how can I get the percetile of each category included in the json?

I tried to at least get every category but I really don't think that the code I made is performatic.

def countCategory(file):
    categorieslist = list()
    for book in file:
        categories = book["categories"]
        for category in categories:
            if category not in categorieslist:
                categorieslist.append(category)
    return categorieslist
    

答案1

得分: 0

你应该使用 "json.load(fp)" 来读取 JSON 文件,并直接使用 extend 来将列表中的每个项目附加到列表中。

from collections import Counter

def countCategory(file):
    categorieslist = list()
    with open(file) as fp:
        books = json.load(fp)
    for book in books:
        categorieslist.extend(book["categories"])
    return categorieslist

def get_category_percentage(categories):
    counter = Counter(categories)
    category_length = len(categories)
    for item, count in counter.items():
        percentage = (count / len(categories)) * 100
        print(f"{item}: {count} ({percentage:.2f}%)")
英文:

You should use "json.load(fp)" to read the json file, and use extend directly to append each item of the list to the list

from collections import Counter

def countCategory(file):
    categorieslist = list()
    with open(file) as fp:
        books = json.load(fp)
    for book in books:
        categories.extend(book["categories"])
    return categorieslist

def get_category_percentage(categories):
    counter = Counter(categories)
    category_length = len(categories)
    for item, count in counter.items():
        percentage = (count / len(categories)) * 100
        print(f"{item}: {count} ({percentage:.2f}%)")

huangapple
  • 本文由 发表于 2023年5月29日 09:20:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76354193.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定