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
python-2.7
spring-data
list
lambda
eclipse-plugin
seedstack
antd
haproxy
typeerror
docker-cloud
qooxdoo
vue-resource
iptables
material-components
stored-procedures
gtk
xades4j
orc
outlook-restapi
pinterest
hidden
scala-ide
adminlte
amazon-mws
data-type-conversion
bsd
batch-rename
ini
ratio
environment
morris.js
onchange
fatfs
dnsmasq
substance
john-the-ripper
osx-lion
quickcheck
counting
payeezy
autoencoder
nppexec
bluemixtools
nsuserdefaults
ms-solver-foundation
unification
python-sounddevice
oauth2
grails-spring-security
notesview
permission-denied
bit-shift
rightnow-crm
mura
google-news
popen
clrs
1010
yargs
controllers
openquery
fuzzer
zynq
right-to-left
sharepoint-apps
mercurial-extension
ui4j
stack-smash
canopy
vdm-sl
text-align
fiware-bosun
kognitio-wx2
hp-idol-ondemand
string.format
utf8-decode
windows-rt
visual-studio-6
webmethod
lumia-imaging-sdk
android-2.3-gingerbread
distributed-r
windows-messages
forio-contour
.htpasswd
hotswap
oxygene
armv6
notifyjs
mp4parser
ntlmv2
idispatch
android-dialog
uitextfielddelegate
java.lang.class
monocross
n-queens
qtextbrowser
drawimage
delphi-prism
space-partitioning
startupscript
rtd
upshot
simile
icicles
eai
version-control-migration
objectinstantiation
account-management
synthesizer
parentid

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App