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('leve l') 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.c om>:
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.c om>:
>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'', 'deptlevelhead er': 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 1331
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('leve l') 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 me give you a little background. this database is kind of
like a human resources database. it collects info about people, where
they work, and if they have any work-related issues where they work.
|
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 yes to
question 1, the total of yes's increases by 1). I've been searching for
a while but I'm not sure I'm searching with the right keywords. Can
anyone direct me to a source I can review to learn how to do this?
Thanks!
--
|
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 useful for storing
constants especially. Generally I find myself either needing to
retrieve the values of constants in an iterative way (as in my
contrived example below). Perhaps even more frequent is given one value
is to look up the matching...
|
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 number of coins
number of quarters
number of dimes
| |
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. I am using Access 2000 and am still learning.
Background info:
Overall, I am creating a database to track and calculate data regarding employee productivity. For each scheduled day, employees have a set number of points to earn. Employees...
|
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"
Can I do like -
total = total + numwords(k)
"Line number %d has %d keywords" % (j, total)
Seems sort of "illegal" in Python?
|
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:
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?
|
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: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
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
|
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |