从每行json数据返回** unique **计数

时间:2019-01-28 07:11:54

标签: python python-3.x

我已经研究了一段时间了,但似乎并没有解决它:我有一块看起来像这样的JSON数据

0    [{'code': '8', 'name': 'Human development'}, {'code': '8', 'name': 'Human development'}
1    [{'code': '1', 'name': 'Economic management'},{'code': '8', 'name': 'Human development'}
2    [{'code': '5', 'name': 'Trade and integration'},{'code': '1', 'name': 'Economic management'}
3    [{'code': '7', 'name': 'Social dev/gender/inclusion'}]

我正在尝试生成每个值的计数,最后是这样的:

Human development : 2
Economic management : 2
Trade and integration : 1
Social dev/gender/inclusion : 1

注意:有些行被编码两次(如第一行),应该只计算一次

我尝试了很多不同的事情,而我最近的经历是

for i in range(0,len(wbp['code'])):
# create a counter for the next step, counting the number of values of each subdict
number = len(wbp['code'][i])-1

#create empty values
dictd = dict()
lis = [] 

#iterate across the sublist 
for j in range (0,number):
    temp_list=[]
    temp_list.append(wbp['code'][i][int(j)]['name'])
    #using set to return only unique values
    lis = tuple(set(temp_list))
    if lis in dictd.keys():
        dictd[lis]+=1
    else:
        dictd[lis]=1
    #lis.append(temp_list)
    #value=[[x,lis.count(x)] for x in lis]
print(dictd)

返回:

{('Human development',): 1}
{('Economic management',): 1}
{('Trade and integration',): 1, ('Public sector governance',): 1, ('Environment and natural resources management',): 1}
{('Social dev/gender/inclusion',): 1}
{('Trade and integration',): 1}
{('Social protection and risk management',): 1}
{('Public sector governance',): 1}
{('Environment and natural resources management',): 1}
{('Rural development',): 1}
{('Public sector governance',): 2}
{('Rural development',): 1}
{('Rural development',): 1, ('Social protection and risk management',): 2}
{}
{('Trade and integration',): 1, ('Environment and natural resources management',): 1}
{('Social protection and risk management',): 2}
{('Rural development',): 1, ('Environment and natural resources management',): 1}
{('Rural development',): 1}
{('Human development',): 1}

这是不对的,因为它不是内部指令之外的工作计数器,这也不是我想要的。我所能想到的是,必须有一种更加疯狂的Python方式来做到这一点...

编辑:似乎我在清晰度方面做得不好:再次在数据集中出现错误,因为像第0行这样的条目存在重复项。 不应该被计算两次。人类发展的预期回报应该是2,而不是3,因为第一行是一个错误。

3 个答案:

答案 0 :(得分:1)

由于输入的详细信息不清楚,因此我假设您的输入如下,并随附以下代码:

    wbp = [[{'code': '8', 'name': 'Human development'}, {'code': '8', 'name': 'Human development'}],
       [{'code': '1', 'name': 'Economic management'}, {'code': '8', 'name': 'Human development'}],
       [{'code': '5', 'name': 'Trade and integration'}, {'code': '1', 'name': 'Economic management'}],
       [{'code': '7', 'name': 'Social dev/gender/inclusion'}]]

dictd = dict()

    for record in wbp:
        names = set([item['name'] for item in record]) # Remove duplicate names using set
        for name in names:
            dictd[name] = dictd.get(name, 0) + 1  # If name not found, then 0 + 1, else count + 1

    print(dictd)

这将导致

  

{

     

“经济管理”:2,

     

“社交开发/性别/共融”:1,

     

“人类发展”:2,

     

“贸易与一体化”:1

     

}

答案 1 :(得分:0)

无法理解第二个for循环temp_list是在每次迭代中创建为空列表的,那么为什么您需要这样做lis = tuple(set(temp_list)) 而不是在变量name中读取它:

name = wbp['code'][i][int(j)]['name']

if name in dictd.keys():
    dictd[name]+=1
else:
    dictd[name]=1

答案 2 :(得分:0)

input = [{'code': '8', 'name': 'Human development'},
        {'code': '8', 'name': 'Human development'},
        {'code': '1', 'name': 'Economic management'},
        {'code': '8', 'name': 'Human development'}, 
        {'code': '5', 'name': 'Trade and integration'},
        {'code': '1', 'name': 'Economic management'},
        {'code': '7', 'name': 'Social dev/gender/inclusion'}]
output = {}
for i in input:
    output[i['name']] = output.get(i['name'], 0) + 1
#Output:{'Social dev/gender/inclusion': 1, 'Economic management': 2, 'Human 
#        development': 3, 'Trade and integration': 1}