# 111.json文件格式 # { "path": ['n1', 'n2', 'n3', 'n1'],# "classes": ['c1', 'c2', 'c3', 'c1'],# "display_names": ['d1', 'd2', 'd3', 'd1']# }import json
from collections import Counter
path ='/home/name/Documents/111.json'withopen(path,"r")asfile:
jf = json.load(file)# jf = json.load(open(path, "r")) # jf is a dict# print(jf)
cnt =dict(Counter(jf['classes']))# print(cnt)
new_jf ={}# build a new dictfor k in jf:
new_jf[k]=[]# print(new_jf)
times =1for i, c inenumerate(jf["classes"]):# now is 298# print(i, c )if cnt[c]<5:
times =10elif cnt[c]<10:
times =3elif cnt[c]<20:
times =2for k in new_jf:
new_jf[k].extend([jf[k][i]]* times)# print(new_jf)# for i ,c in enumerate(new_jf["classes"]): # after aug is 1451# print(i, c)withopen(path,"w", encoding='utf-8')as f:
json.dump(new_jf, f, indent=4, ensure_ascii=False)# json.dump()传递两个参数 第一个要写入的,第二个要存储的位置# indent表示间隔的长度# ensure_ascii=False 是否要转为ASCII码# 我要把new_jf写入path的路径中withopen(path,"r")as fm:# 载入json,查看是否已经改好
data = json.load(fm)# print(data)
cnt =dict(Counter(data["classes"]))print(cnt)
#################################################### demo_testimport json
from collections import Counter
dic ={"path":['n1','n2','n3','n1'],"classes":['c1','c2','c3','c1'],"display_names":['d1','d2','d3','d1']}
new_dic ={}# 新建一个dictfor k in dic:# 建立dict里的结构
new_dic[k]=[]print(new_dic)
cnt =dict(Counter(dic['classes']))print(cnt)# for i in cnt.keys(): # 此法无法由'c1'去确定'n1','d1', --> 给他们编号,通过位置编号确定各个对应的元素# for k in dic:# print(k)# new_dic[k].extend()print(list(enumerate(dic["classes"])))for i, c inenumerate(dic["classes"]):print(i,c)if cnt[c]==2:
times =3else:
times =5for k in new_dic:
new_dic[k].extend([dic[k][i]]* times)print(new_dic)