json文件处理_将不同数量的Class分别扩展成原数量的多倍

# 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'

with open(path, "r") as file:
    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 dict
for k in jf:
    new_jf[k] = []
# print(new_jf)

times =1
for i, c in enumerate(jf["classes"]):  # now is 298
    # print(i, c )
    if cnt[c] < 5:
        times = 10
    elif cnt[c] < 10:
        times = 3
    elif cnt[c] < 20:
        times = 2
    for 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)

with open(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的路径中

with open(path,"r") as fm:  # 载入json,查看是否已经改好
    data = json.load(fm)
# print(data)
cnt = dict(Counter(data["classes"]))
print(cnt)
####################################################     demo_test
import json
from collections import Counter

dic = {"path": ['n1', 'n2', 'n3', 'n1'],
       "classes": ['c1', 'c2', 'c3', 'c1'],
       "display_names": ['d1', 'd2', 'd3', 'd1']
       }

new_dic = {}  # 新建一个dict
for 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 in enumerate(dic["classes"]):
    print(i,c)
    if cnt[c] == 2:
        times = 3
    else:
        times = 5
    for k in new_dic:
        new_dic[k].extend([dic[k][i]] * times)
print(new_dic)
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值