473,549 Members | 2,408 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help counting the total number of dictionaries inside a list thatcontain a specified key value

Hi All,

I have the following list containing dictionaries and I would like to
be able to count the total number of dictionaries I have that contain
a certain value set for the 'level' key:

[{'mod_title': u'Introduction to Human Anatomy', 'level': u'1',
'modcode': u'ANAT1003', 'deptleveltext' : u'', 'deptlevelheade r':
u'Level 1 Courses', 'subj_code': u'AE'}, {'mod_title': u'Developmental
Neurobiology', 'level': u'2', 'modcode': u'ANAT2008', 'deptleveltext' :
u'', 'deptlevelheade r': u'Level 2 Courses', 'subj_code': u'AE'},
{'mod_title': u'Human Neuroanatomy', 'level': u'2', 'modcode':
u'ANAT2010', 'deptleveltext' : u'', 'deptlevelheade r': u'Level 2
Courses', 'subj_code': u'AE'}, {'mod_title': u'Human Anatomy and
Embryology', 'level': u'2', 'modcode': u'ANAT2050', 'deptleveltext' :
u'', 'deptlevelheade r': u'Level 2 Courses', 'subj_code': u'AE'},
{'mod_title': u'Ethics of Fertility and Embryo Research', 'level':
u'2', 'modcode': u'ANAT2099', 'deptleveltext' : u'', 'deptlevelheade r':
u'Level 2 Courses', 'subj_code': u'AE'}, {'mod_title': u"Man's Place
in Nature 1750-1900", 'level': u'23', 'modcode': u'HMED3001',
'deptleveltext' : u'', 'deptlevelheade r': u'Level 2/3 Courses',
'subj_code': u'AE'}, {'mod_title': u'Medicine, Disease and Society,
Antiquity to Renaissance ', 'level': u'23', 'modcode': u'HMED3003',
'deptleveltext' : u'', 'deptlevelheade r': u'Level 2/3 Courses',
'subj_code': u'AE'}, {'mod_title': u'Madness and Society', 'level':
u'23', 'modcode': u'HMED3004', 'deptleveltext' : u'',
'deptlevelheade r': u'Level 2/3 Courses', 'subj_code': u'AE'}]

For example I'd like to kow how many dictionaries there are with a
level 1, 2 , 23 & 3 etc. How would one go about achieveing this?

Hope someone can help.

Cheers

Jon
Aug 12 '08 #1
2 998
Jon Bowlas wrote:
I have the following list containing dictionaries and I would like to
be able to count the total number of dictionaries I have that contain
a certain value set for the 'level' key:
For example I'd like to kow how many dictionaries there are with a
level 1, 2 , 23 & 3 etc. How would one go about achieveing this?
from collections import defaultdict
freq = defaultdict(int )
for course in courses:
freq[course[u"level"]] += 1
print freq

Peter
Aug 12 '08 #2
On Aug 12, 9:09 pm, "Jon Bowlas" <m...@jonbowlas .comwrote:
Hi All,

I have the following list containing dictionaries and I would like to
be able to count the total number of dictionaries I have that contain
a certain value set for the 'level' key:

[snip]
>
For example I'd like to kow how many dictionaries there are with a
level 1, 2 , 23 & 3 etc. How would one go about achieveing this?
Assuming:
thelist = [etc etc etc]

q = set(['1']); print q, sum(d.get('leve l') in q for d in thelist)
q = set(['23']); print q, sum(d.get('leve l') in q for d in thelist)
q = set(['2', '23']); print q, sum(d.get('leve l') in q for d in
thelist)

produces:
set(['1']) 1
set(['23']) 3
set(['2', '23']) 7

Is that what you wanted? If you are sure that each dict will have a
'level' key, you can use d['level'] instead of d.get('level').

Cheers,
John
Aug 12 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
3259
by: stu_gots | last post by:
I have been losing sleep over this puzzle, and I'm convinced my train of thought is heading in the wrong direction. It is difficult to explain my circumstances, so I will present an identical make-believe challenge in order to avoid confusing the issue further. Suppose I was hosting a dinner and I wanted to invite exactly 12 guests from my...
8
5457
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- Hello, I have a very simple problem but cannot seem to figure it out. I have a very simple php script that sends a test email to myself. When I...
9
2344
by: santosh | last post by:
Hello all, I've put together a small program to count the number of characters and 'words' in a text file. The minimum length of a word, (in terms of no. of characters), as well as word delimiting characters can be specified on the command line. The default delimiting characters built into the program are space, newline, tab, carriage...
1
3697
by: Rahul | last post by:
Hi Everybody I have some problem in my script. please help me. This is script file. I have one *.inq file. I want run this script in XML files. But this script errors shows . If u want i am attach this script files and inq files. I cant understand this error. Please suggest me. You can talk with my yahoo id b_sahoo1@yahoo.com. Now i am...
4
2185
by: naknak4 | last post by:
Introduction This assignment requires you to develop solutions to the given problem using several different approaches (which actually involves using three different STL containers). You will implement all three techniques as programs. In these programs, as well as solving the problem, you will also measure how long the program takes to...
6
6566
by: karafire2003 | last post by:
Hey there i was wondering if someone can help me with my program. I thought i had it down...but i'm having some problems. Here's the assignment. Write a C function named change() that accepts a floating point number of total coins and the addresses of the integer variables named quarters, dimes, nickels, and pennies. The function should...
1
1997
by: mshroom12 | last post by:
Hello to all. I am having difficulty trying to do this Java project using Eclipse. The following is what I have to do. Election Day It's almost election day and the election officials need a program to help tally election results. There are two candidates for office -- Polly Tichen and Ernest Orator. The program's job is to take as input the...
0
1037
by: Christian Heimes | last post by:
Jon Bowlas wrote: sum(u'Level 2 Courses' in dct for dct in yourlist) Christian
0
128
by: Jon Bowlas | last post by:
Many thanks for all your reponses, much appreciated. I'll get back to you on which is the best for me. BTW - yes John thats exactly what I wanted. Cheers Jon
0
7467
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7736
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7500
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7827
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5385
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5110
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3514
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1961
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
783
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.