469,920 Members | 2,331 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,920 developers. It's quick & easy.

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

Hrmm, any ideas why I'd be getting 'SyntaxError: invalid syntax' for
both of these?
sum(u'Level 2 Courses' in dct for dct in yourlist)
q = set(['1']); print q, sum(d.get('level') in q for d in thelist)
The error occurs at the 'for'

I'm afraid I can't use Peters suggestion as I'm using python 2.3 and
it doesn't have the collection module. Thanks anyway.

Cheers

Jon

2008/8/12 Jon Bowlas <me@jonbowlas.com>:
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

Jon Bowlas wrote:
>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.

sum(u'Level 2 Courses' in dct for dct in yourlist)

Christian

2008/8/12 Jon Bowlas <me@jonbowlas.com>:
>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'', 'deptlevelheader':
u'Level 1 Courses', 'subj_code': u'AE'}, {'mod_title': u'Developmental
Neurobiology', 'level': u'2', 'modcode': u'ANAT2008', 'deptleveltext':
u'', 'deptlevelheader': u'Level 2 Courses', 'subj_code': u'AE'},
{'mod_title': u'Human Neuroanatomy', 'level': u'2', 'modcode':
u'ANAT2010', 'deptleveltext': u'', 'deptlevelheader': u'Level 2
Courses', 'subj_code': u'AE'}, {'mod_title': u'Human Anatomy and
Embryology', 'level': u'2', 'modcode': u'ANAT2050', 'deptleveltext':
u'', 'deptlevelheader': 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'', 'deptlevelheader':
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'', 'deptlevelheader': 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'', 'deptlevelheader': u'Level 2/3 Courses',
'subj_code': u'AE'}, {'mod_title': u'Madness and Society', 'level':
u'23', 'modcode': u'HMED3004', 'deptleveltext': u'',
'deptlevelheader': 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
1 1146
Jon Bowlas wrote:
Hrmm, any ideas why I'd be getting 'SyntaxError: invalid syntax' for
both of these?
>sum(u'Level 2 Courses' in dct for dct in yourlist)
q = set(['1']); print q, sum(d.get('level') in q for d in thelist)

The error occurs at the 'for'
I'm afraid I can't use Peters suggestion as I'm using python 2.3 and
it doesn't have the collection module. Thanks anyway.
John's suggestion uses a generator expression (new in 2.4). Try a list
comprehension instead:

q = set(['1']); print q, sum([d.get('level') in q for d in thelist])

You can modify my code to use normal dictionaries:

freq = {}
for course in courses:
level = course[u"level"]
freq[level] = freq.get(level, 0) + 1
print freq

Peter
Aug 12 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Frohnhofer, James | last post: by
3 posts views Thread by Megan | last post: by
1 post views Thread by Jerry | last post: by
2 posts views Thread by David Pratt | last post: by
3 posts views Thread by viewsonic | last post: by
11 posts views Thread by Andy | last post: by
reply views Thread by Waqarahmed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.