counting


python: count values of dictionary


I'm working with python and I need to find in a dictionary how many values each key has.
this is my dictionary:
{2: [(1, 1)], 3: [(2, 1), (2, 1)], 4: [(1, 3), (3, 1), (3, 1)], 5: [(1, 4), (2, 3), (2, 3), (4, 1)], 6: [(1, 5), (2, 4), (2, 4), (3, 3), (3, 3)], 7: [(1, 6), (2, 5), (2, 5), (3, 4), (3, 4), (4, 3)], 8: [(2, 6), (2, 6), (3, 5), (3, 5), (4, 4)], 9: [(1, 8), (3, 6), (3, 6), (4, 5)], 10: [(2, 8), (2, 8), (4, 6)], 11: [(3, 8), (3, 8)], 12: [(4, 8)]}
I need to find how many values there are for each key:
so it will be like:
{2: 1, 3: 2, 4: 3, 5: 4, 6: 5, 7: 6, 8: 5, 9: 4, 10: 3, 11: 2, 12: 1}
I've tried to make it work for a long time
thanks in advance
Not sure if this is what you're looking for?
def getcountvalue(d):
newdict = {}
for key, value in d.items():
newdict[key] = len(value)
return newdict
d = {2: [(1, 1)], 3: [(2, 1), (2, 1)], 4: [(1, 3), (3, 1), (3, 1)], 5: [(1, 4), (2, 3), (2, 3), (4, 1)], 6: [(1, 5), (2, 4), (2, 4), (3, 3), (3, 3)], 7: [(1, 6), (2, 5), (2, 5), (3, 4), (3, 4), (4, 3)], 8: [(2, 6), (2, 6), (3, 5), (3, 5), (4, 4)], 9: [(1, 8), (3, 6), (3, 6), (4, 5)], 10: [(2, 8), (2, 8), (4, 6)], 11: [(3, 8), (3, 8)], 12: [(4, 8)]}
newd = getcountvalue(d)

Related Links

How to answer queries of type l,r,k which finds number of elements in an array in range l to r which occurs atleast k times?
Dafny and counting of occurences
Counting the number of capital letters in each row in [R]
python: count values of dictionary
How to apply hyperloglog to a timeseries stream
Counting the number of occurrences of C in each line and outputting this number plus the total number of characters in that line
Digit Counting Issue in C++ Program
Binary Strings of the form *111*

Categories

HOME
cucumber
schema.org
paypal-ipn
writefile
material-components
azure-data-lake
ipv6
scheduler
cname
android-externalstorage
live-streaming
webstore
l20n
jaspersoft-studio
home
pe
batch-rename
perfino
quartz.net
mod-wsgi
unrar
ruby-daemons
sendmessage
javax.persistence
amazon-iam
audit
netbios
actframework
nest
android-alarms
gitolite
flipkart
dbcc
destroy
section508
ssi
supertest
jgrapht
ptvs
ltrace
front-camera
adobe-reader
fitbit
gawk
deviare
java-2d
tsqlt
piping
flush
flask-security
enunciate
android-mapview
self
nastran
screen-orientation
360-degrees
android-handler
relational-model
kefir.js
actionpack
tree-structure
whitespace-language
tidyr
phonegap-facebook-plugin
famo.us
cakephp-2.2
device-admin
adwords-apiv201402
window-resize
tabris
ecos
full-text-indexing
distributed-r
git-reset
prism.js
zend-search-lucene
roxy-fileman
usb-flash-drive
typesafe-stack
spring-security-acl
sensormanager
visual-c++-2005
mongo-jackson-mapper
xcode4.5
workflow-manager-1.x
jzmq
wcf-callbacks
acpi
glx
server-variables
space-partitioning
cassini
tabbarcontroller
principles
motif
version-control-migration

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile