计算python字典中值的重复

时间:2013-03-14 06:15:11

标签: python django dictionary

我有一个以下格式的字典。在这本词典中,存在不同种类的区域,但是存在多次。我想从中生成另一个字典,其中包含一个附加键“Count”,该键将包含区域(即“Full Run”或“Half Run”或“Semi Run”)重复的次数。

[
{'zip_zone': u'Full Run', 'zipcode': u'14042', 'longitude': -78.516154}, 
{'zip_zone': u'Full Run', 'zipcode': u'14101', 'longitude': -78.51734}, 
{'zip_zone': u'Full Run', 'zipcode': u'14706', 'longitude': -78.493761}, 
{'zip_zone': u'Half Run', 'zipcode': u'14709', 'longitude': -78.024817}, 
{'zip_zone': u'Semi Run', 'zipcode': u'14711', 'longitude': -78.119974}, 
{'zip_zone': u'Full Run', 'zipcode': u'14714', 'longitude': -78.256921}, 
{'zip_zone': u'Half Run', 'zipcode': u'14715', 'longitude': -78.157392}, 
{'zip_zone': u'Semi Run', 'zipcode': u'14717', 'longitude': -78.210567}, 
{'zip_zone': u'Semi Run', 'zipcode': u'14719', 'longitude': -78.86951}, 
{'zip_zone': u'Half Run', 'zipcode': u'14727', 'longitude': -78.268103}, 
{'zip_zone': u'Semi Run', 'zipcode': u'14731', 'longitude': -78.658909}, 
{'zip_zone': u'Half Run', 'zipcode': u'14735', 'longitude': -78.087607}, 
{'zip_zone': None, 'zipcode': u'14737', 'longitude': -78.431625}, 
{'zip_zone': u'Semi Run', 'zipcode': u'14739', 'longitude': -78.139046}, 
{'zip_zone': u'Semi Run', 'zipcode': u'14741', 'longitude': -78.5907}, 
{'zip_zone': u'Special Run', 'zipcode': u'14743', 'longitude': -78.4098}, 
{'zip_zone': u'Special Run', 'zipcode': u'14744', 'longitude': -78.167853}, 
{'zip_zone': u'Half Run', 'zipcode': u'14748', 'longitude': -78.639987}, 
{'zip_zone': u'Semi Run', 'zipcode': u'14753', 'longitude': -78.640416}, 
{'zip_zone': u'Special Run', 'zipcode': u'14754', 'longitude': -78.18395}, 
{'zip_zone': u'Special Run', 'zipcode': u'14755', 'longitude': -78.800866}, 
{'zip_zone': u'Half Run', 'zipcode': u'14760', 'longitude': -78.426015}, 
]

输出字典应该看起来像

[
{'zip_zone': u'Full Run', 'zipcode': u'14042', 'longitude': -78.516154, 'count': 4}, 
{'zip_zone': u'Full Run', 'zipcode': u'14101', 'longitude': -78.51734, 'count': 4}, 
{'zip_zone': u'Full Run', 'zipcode': u'14706', 'longitude': -78.493761, 'count': 4}, 
{'zip_zone': u'Half Run', 'zipcode': u'14709', 'longitude': -78.024817, 'count': 6}, 
{'zip_zone': u'Semi Run', 'zipcode': u'14711', 'longitude': -78.119974, 'count': 7}, 
{'zip_zone': u'Full Run', 'zipcode': u'14714', 'longitude': -78.256921, 'count': 4}, 
{'zip_zone': u'Half Run', 'zipcode': u'14715', 'longitude': -78.157392, 'count': 6}, 
{'zip_zone': u'Semi Run', 'zipcode': u'14717', 'longitude': -78.210567, 'count': 7}, 
{'zip_zone': u'Semi Run', 'zipcode': u'14719', 'longitude': -78.86951, 'count': 7}, 
{'zip_zone': u'Half Run', 'zipcode': u'14727', 'longitude': -78.268103, 'count': 6}, 
{'zip_zone': u'Semi Run', 'zipcode': u'14731', 'longitude': -78.658909, 'count': 7}, 
{'zip_zone': u'Half Run', 'zipcode': u'14735', 'longitude': -78.087607, 'count': 6}, 
{'zip_zone': None, 'zipcode': u'14737', 'longitude': -78.431625, 'count': 0}, 
{'zip_zone': u'Semi Run', 'zipcode': u'14739', 'longitude': -78.139046, 'count': 7}, 
{'zip_zone': u'Semi Run', 'zipcode': u'14741', 'longitude': -78.5907, 'count': 7}, 
{'zip_zone': u'Special Run', 'zipcode': u'14743', 'longitude': -78.4098, 'count': 4}, 
{'zip_zone': u'Special Run', 'zipcode': u'14744', 'longitude': -78.167853, 'count': 4}, 
{'zip_zone': u'Half Run', 'zipcode': u'14748', 'longitude': -78.639987, 'count': 6}, 
{'zip_zone': u'Semi Run', 'zipcode': u'14753', 'longitude': -78.640416, 'count': 7}, 
{'zip_zone': u'Special Run', 'zipcode': u'14754', 'longitude': -78.18395, 'count': 4}, 
{'zip_zone': u'Special Run', 'zipcode': u'14755', 'longitude': -78.800866, 'count': 4}, 
{'zip_zone': u'Half Run', 'zipcode': u'14760', 'longitude': -78.426015, 'count': 6}, 
]

4 个答案:

答案 0 :(得分:5)

这是Python集合模块中Counter类的一个很好的用例。

import collections

# u is your input list of dictionaries, entries in u will be modified in place

c = collections.Counter(e["zip_zone"] for e in u)
for e in u:
    e["count"] = c[e["zip_zone"]]

答案 1 :(得分:0)

也许不是很漂亮,但您可以尝试使用defaultdict

from collections import defaultdict

output = defaultdict(list)

for line in origData:
    output[line['zip_zone']].append(line)

for line in origData:
    line['Count'] = len(output[line['zip_zone']])

print origData

答案 2 :(得分:0)

我不太确定你的问题,但是下面的代码可以做你想要的问题:

input = [
{'zip_zone': u'Full Run', 'zipcode': u'14042', 'longitude': -78.516154},
{'zip_zone': u'Full Run', 'zipcode': u'14101', 'longitude': -78.51734},
{'zip_zone': u'Full Run', 'zipcode': u'14706', 'longitude': -78.493761},
{'zip_zone': u'Half Run', 'zipcode': u'14709', 'longitude': -78.024817},
{'zip_zone': u'Semi Run', 'zipcode': u'14711', 'longitude': -78.119974},
{'zip_zone': u'Full Run', 'zipcode': u'14714', 'longitude': -78.256921},
{'zip_zone': u'Half Run', 'zipcode': u'14715', 'longitude': -78.157392},
{'zip_zone': u'Semi Run', 'zipcode': u'14717', 'longitude': -78.210567},
{'zip_zone': u'Semi Run', 'zipcode': u'14719', 'longitude': -78.86951},
{'zip_zone': u'Half Run', 'zipcode': u'14727', 'longitude': -78.268103},
{'zip_zone': u'Semi Run', 'zipcode': u'14731', 'longitude': -78.658909},
{'zip_zone': u'Half Run', 'zipcode': u'14735', 'longitude': -78.087607},
{'zip_zone': None, 'zipcode': u'14737', 'longitude': -78.431625},
{'zip_zone': u'Semi Run', 'zipcode': u'14739', 'longitude': -78.139046},
{'zip_zone': u'Semi Run', 'zipcode': u'14741', 'longitude': -78.5907},
{'zip_zone': u'Special Run', 'zipcode': u'14743', 'longitude': -78.4098},
{'zip_zone': u'Special Run', 'zipcode': u'14744', 'longitude': -78.167853},
{'zip_zone': u'Half Run', 'zipcode': u'14748', 'longitude': -78.639987},
{'zip_zone': u'Semi Run', 'zipcode': u'14753', 'longitude': -78.640416},
{'zip_zone': u'Special Run', 'zipcode': u'14754', 'longitude': -78.18395},
{'zip_zone': u'Special Run', 'zipcode': u'14755', 'longitude': -78.800866},
{'zip_zone': u'Half Run', 'zipcode': u'14760', 'longitude': -78.426015},
];
output=[];
zipZoneCnt={};
for item in input:
        if item['zip_zone'] in zipZoneCnt.keys():
                zipZoneCnt[item['zip_zone']]+=1;
        else:
                zipZoneCnt[item['zip_zone']]=1;
zipZoneCnt[None]=0;
for item in input:
        item['count']=zipZoneCnt[item['zip_zone']];
print zipZoneCnt;
for item in input:
        print item;

答案 3 :(得分:0)

collections.Counter救援。

from collections import Counter
a = [
 {'zip_zone': u'Full Run', 'zipcode': u'14042', 'longitude': -78.516154}, 
 {'zip_zone': u'Full Run', 'zipcode': u'14101', 'longitude': -78.51734}, 
 {'zip_zone': u'Full Run', 'zipcode': u'14706', 'longitude': -78.493761}, 
 {'zip_zone': u'Half Run', 'zipcode': u'14709', 'longitude': -78.024817}, 
 {'zip_zone': u'Semi Run', 'zipcode': u'14711', 'longitude': -78.119974},
]

# to obtain the counts:
c = Counter( x['zip_zone'] for x in a )
c
= Counter({u'Full Run': 3, u'Semi Run': 1, u'Half Run': 1})

# to update original structure in place:
for x in a:
     x['count'] = c[x['zip_zone']]

a

[{'count': 3,
  'longitude': -78.516154,
  'zip_zone': u'Full Run',
  'zipcode': u'14042'},
 {'count': 3,
  'longitude': -78.51734,
  'zip_zone': u'Full Run',
  'zipcode': u'14101'},
 {'count': 3,
  'longitude': -78.493761,
  'zip_zone': u'Full Run',
  'zipcode': u'14706'},
 {'count': 1,
  'longitude': -78.024817,
  'zip_zone': u'Half Run',
  'zipcode': u'14709'},
 {'count': 1,
  'longitude': -78.119974,
  'zip_zone': u'Semi Run',
  'zipcode': u'14711'}]