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 1 1234
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Frohnhofer, James |
last post by:
My initial problem was to initialize a bunch of dictionaries at the start of a
function.
I did not want to do
def fn():
a = {}
b = {}
c = {}
. . .
z = {}
|
by: Megan |
last post by:
hi everybody-
i'm having a counting problem i hope you guys and gals could give me
some help with. i have a query that retrieves a bevy of information
from several different tables.
first let...
|
by: Jerry |
last post by:
We have a 10-question quiz for kids, each question being a yes or no
answer using radio selections. I'd like to keep a current total of
yes's and no's at the bottom of the quiz (if the user selects...
|
by: David Pratt |
last post by:
Hi. I like working with lists of dictionaries since order is preserved
in a list when I want order and the dictionaries make it explicit what
I have got inside them. I find this combination very...
|
by: viewsonic |
last post by:
Help, im a student that has to write a program for counting coins. Half of the program works but the other half doesn.t
should have the following parameters.
output is:
Name
Date
total...
|
by: jennwilson |
last post by:
Ok - So, I am back.
I would like to count the number of times a specific record appears in a field from one table in my query and then use that value in the same query to calculate an average....
|
by: Andy |
last post by:
Hi, the file below will print all the keywords in a file and also the
line # of the keyword. What I couldn't figure out is to count those
keywords per line. For example - "Line #1 has 3 keywords"
...
|
by: Jon Bowlas |
last post by:
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:
...
|
by: Christian Heimes |
last post by:
Jon Bowlas wrote:
sum(u'Level 2 Courses' in dct for dct in yourlist)
Christian
|
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
|
by: Kemmylinns12 |
last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
|
by: Naresh1 |
last post by:
What is WebLogic Admin Training?
WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
|
by: antdb |
last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine
In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
|
by: Arjunsri |
last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
|
by: WisdomUfot |
last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
|
by: Matthew3360 |
last post by:
Hi,
I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
|
by: BLUEPANDA |
last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
|
by: Ricardo de Mila |
last post by:
Dear people, good afternoon...
I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control.
Than I need to discover what...
|
by: ezappsrUS |
last post by:
Hi,
I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
| |