Re: list + dictionary searching

2008-09-01 Thread Wojtek Walczak
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.html#SECTION00620
http://docs.python.org/tut/node7.html#SECTION00750

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/
--
http://mail.python.org/mailman/listinfo/python-list


Re: list + dictionary searching

2008-09-01 Thread Bruno Desthuilliers

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
--
http://mail.python.org/mailman/listinfo/python-list


list + dictionary searching

2008-09-01 Thread Manoj
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

--
http://mail.python.org/mailman/listinfo/python-list