473,385 Members | 1,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

list + dictionary searching

Hello All,

I am very new to python. Any help will be highly appreciated. Thanks

I have a list of dictionaries:

a = [{'username': u'John Wang', 'user_utilization': 1.0, 'month': 9,
'user_id': 4, 'year': 2008}, {'username': u'John Wang',
'user_utilization': 1.0, 'month': 10, 'user_id': 4, 'year': 2008},
{'username': u' ', 'user_utilization': 1.0, 'month': 9, 'user_id': 1,
'year': 2008}]

I would like to :

search dictionaries within this list
create a new list with dictionaries which gives 1 dictionary for every
user with month_year as a key and utilization for that month as a
value

Please give your thoughts

Thanks,
Manoj

Sep 1 '08 #1
2 1113
Manoj a écrit :
Hello All,

I am very new to python. Any help will be highly appreciated. Thanks

I have a list of dictionaries:

a = [{'username': u'John Wang', 'user_utilization': 1.0, 'month': 9,
'user_id': 4, 'year': 2008}, {'username': u'John Wang',
'user_utilization': 1.0, 'month': 10, 'user_id': 4, 'year': 2008},
{'username': u' ', 'user_utilization': 1.0, 'month': 9, 'user_id': 1,
'year': 2008}]

I would like to :

search dictionaries within this list
Care to elaborate ???
create a new list with dictionaries which gives 1 dictionary for every
user with month_year as a key and utilization for that month as a
value
assuming the user_id/month/year combination is unique:

from collections import defaultdict
users = defaultdict(dict)

for record in a:
month_year = "%(month)s_%(year)s" % record
users[record['user_id']][month_year] = record['user_utilization']

print users.values()

HTH
Sep 1 '08 #2
On Mon, 1 Sep 2008 03:14:22 -0700 (PDT), Manoj wrote:
I would like to :

search dictionaries within this list
create a new list with dictionaries which gives 1 dictionary for every
user with month_year as a key and utilization for that month as a
value

Please give your thoughts
Reading about for loop seems like a good idea. You can easily
create a set of functions that gives you direct access to the
information you need.

http://docs.python.org/tut/node6.htm...00000000000000
http://docs.python.org/tut/node7.htm...00000000000000

Give it a try. Try to design some functions that let you
view and add new lists/dictionaries to your variables.

--
Regards,
Wojtek Walczak,
http://tosh.pl/gminick/
Sep 1 '08 #3

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

Similar topics

5
by: Odd-R. | last post by:
I have this list: All the dictionaries of this list are of the same form, and all the oids are distinct. If I have an oid and the list, how is the simplest way of getting the dictionary that...
4
by: techiepundit | last post by:
I'm a Python newbie who just started learning the language a few weeks ago. So these are beginner questions. I have a list of sockets that I use for select.select calls like this: ...
6
by: rh0dium | last post by:
Hi all, I am having a bit of difficulty in figuring out an efficient way to split up my data and identify the unique pieces of it. list= Now I want to split each item up on the "_" and...
6
by: buzzweetman | last post by:
Many times I have a Dictionary<string, SomeTypeand need to get the list of keys out of it as a List<string>, to pass to a another method that expects a List<string>. I often do the following: ...
7
by: Sehboo | last post by:
We have several generic List objects in our project. Some of them have about 1000 items in them. Everytime we have to find something, we have to do a for loop. There is one method which does the...
8
by: Guy | last post by:
Is there a better way to search identical elements in a sorted array list than the following: iIndex = Array.BinarySearch( m_Array, 0, m_Array.Count, aSearchedObject ); aFoundObject= m_Array;...
9
by: TheFlyingDutchman | last post by:
I am trying to use a database written in Python called buzhug. In looking at some of the functions I see this prototype: def create(self,*fields,**kw): I am not clear on what the * and the...
20
by: Seongsu Lee | last post by:
Hi, I have a dictionary with million keys. Each value in the dictionary has a list with up to thousand integers. Follow is a simple example with 5 keys. dict = {1: , 2: , 900000: , 900001:...
2
by: Terry Reedy | last post by:
SUBHABRATA, I recommend you study this excellent response carefully. castironpi wrote: It starts with a concrete test case -- an 'executable problem statement'. To me, this is cleared and...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
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,...
0
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...
0
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,...
0
jinu1996
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...

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.