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

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

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

问题

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

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

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

  1. def countCategory(file):
  2. categorieslist = list()
  3. for book in file:
  4. categories = book["categories"]
  5. for category in categories:
  6. if category not in categorieslist:
  7. categorieslist.append(category)
  8. 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.

  1. def countCategory(file):
  2. categorieslist = list()
  3. for book in file:
  4. categories = book["categories"]
  5. for category in categories:
  6. if category not in categorieslist:
  7. categorieslist.append(category)
  8. return categorieslist

答案1

得分: 0

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

  1. from collections import Counter
  2. def countCategory(file):
  3. categorieslist = list()
  4. with open(file) as fp:
  5. books = json.load(fp)
  6. for book in books:
  7. categorieslist.extend(book["categories"])
  8. return categorieslist
  9. def get_category_percentage(categories):
  10. counter = Counter(categories)
  11. category_length = len(categories)
  12. for item, count in counter.items():
  13. percentage = (count / len(categories)) * 100
  14. 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

  1. from collections import Counter
  2. def countCategory(file):
  3. categorieslist = list()
  4. with open(file) as fp:
  5. books = json.load(fp)
  6. for book in books:
  7. categories.extend(book["categories"])
  8. return categorieslist
  9. def get_category_percentage(categories):
  10. counter = Counter(categories)
  11. category_length = len(categories)
  12. for item, count in counter.items():
  13. percentage = (count / len(categories)) * 100
  14. 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:

确定