将json转换为python中的字典,在数据上进行累积

将json转换为python中的字典,在数据上进行累积,python,dictionary,Python,Dictionary,给定一个json文件data.json,我想通过使用三个不同的函数将存储在变量data_列表中的json文件缩减为三个不同的字典crime1和crime2 [{"Region": "South", "State": "ALABAMA", "City": "Athens", "Population": "25603", "

给定一个json文件data.json,我想通过使用三个不同的函数将存储在变量data_列表中的json文件缩减为三个不同的字典crime1和crime2

    [{"Region": "South", "State": "ALABAMA", "City": "Athens", "Population": "25603", "Murder": "1", "Rape": "1", "Robbery": "16", "Assault": "0", "Burglary": "90", "Theft": "538", "Vehicle_Theft": "8"}, {"Region": "South", "State": "ALABAMA", "City": "Atmore", "Population": "10021", "Murder": "0", "Rape": "3", "Robbery": "14", "Assault": "76", "Burglary": "86", "Theft": "315", "Vehicle_Theft": "12"}]
我把它加载到一个变量中

    with open('/content/data_crime.json', 'r') as f:
          data_list = json.load(f)
我想将数据列表简化为三个字典:
按地区划分的谋杀
按地区划分的暴力
,以及 按地区划分的非暴力

创建字典在
数据列表上迭代
使用累积模式创建字典
暴力犯罪
谋杀
袭击
非暴力
盗窃
车辆盗窃

我通过使用函数来制作这三本词典

    function takes three parameters:
         
    Key: region or state
    crime : 'Murder' 
    data_list:the list containing dictionaries for each city
给你:

从集合导入defaultdict
导入json
按地区划分的谋杀=defaultdict(int)
暴力每区域=默认dict(int)
非暴力地区=defaultdict(int)
将open('/content/data_crime.json')作为f:
data_list=json.load(f)
对于数据列表中的行:
区域=行['region']
谋杀按区域[区域]+=int(row.get('谋杀',0))
暴力地区[地区]+=int(row.get('谋杀',0))+int(row.get('攻击',0))
非暴力地区[地区]+=int(row.get('盗窃',0))+int(row.get('车辆盗窃',0))
给你:

从集合导入defaultdict
导入json
按地区划分的谋杀=defaultdict(int)
暴力每区域=默认dict(int)
非暴力地区=defaultdict(int)
将open('/content/data_crime.json')作为f:
data_list=json.load(f)
对于数据列表中的行:
区域=行['region']
谋杀按区域[区域]+=int(row.get('谋杀',0))
暴力地区[地区]+=int(row.get('谋杀',0))+int(row.get('攻击',0))
非暴力地区[地区]+=int(row.get('盗窃',0))+int(row.get('车辆盗窃',0))

为什么不把它做成一本字典,其中的键是城市名称

然后这样做,它可以很容易地调整,以获得像你一样的输入

with open('data_crime.json', 'r') as File:
    FileData = json.load(File)
    ExitData = {} # empty dict
    nonViolent = ['Robbery', 'Burglary', 'etc..']
    Violent = ['Assult', 'Rape']
    for i in FileData:
        # i is the key or in this case the city name
        numOfNonViolent = 0
        for j in nonViolent:
            numOfNonViolent += FileData[i][j]
        numOfViolent = 0
        for j in Violent:
            numOfViolent += FileData[i][j]
        
        # will make a new key for ExitData the key is the city name
        ExitData[i] = {
            'Violent Crime' : numOfViolent
            'NonViolent Crime' : numOfNonViolent
            'Murder' : FileData[i]['Murder']
        }

为什么不把它做成一本字典,里面的键是城市的名字

然后这样做,它可以很容易地调整,以获得像你一样的输入

with open('data_crime.json', 'r') as File:
    FileData = json.load(File)
    ExitData = {} # empty dict
    nonViolent = ['Robbery', 'Burglary', 'etc..']
    Violent = ['Assult', 'Rape']
    for i in FileData:
        # i is the key or in this case the city name
        numOfNonViolent = 0
        for j in nonViolent:
            numOfNonViolent += FileData[i][j]
        numOfViolent = 0
        for j in Violent:
            numOfViolent += FileData[i][j]
        
        # will make a new key for ExitData the key is the city name
        ExitData[i] = {
            'Violent Crime' : numOfViolent
            'NonViolent Crime' : numOfNonViolent
            'Murder' : FileData[i]['Murder']
        }

你在解决这个问题上的尝试是什么?预期的结果是什么?@BalajiAmbresh输出了三个不同的字典,分别显示了谋杀和暴力,和json的非暴力地区file@Sushanth我为每个问题制作了字典,但我没有通过制作函数得到想要的结果。你在解决这个问题上的尝试是什么?预期的结果是什么?@BalajiAmbresh输出了三个不同的字典,分别显示了谋杀和暴力,和json的非暴力地区file@Sushanth我为每个问题都编了字典,但通过编写函数并没有得到预期的结果