英文:
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}%)")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论