Link to home
Start Free TrialLog in
Avatar of punit
punit

asked on

Python list of tuples

Hi,

I have a list of tuples and would like to create a list as described below:
Given list of tuples:
[(datetime.datetime(2008, 6, 21, 0, 0), 26, 'A'),
(datetime.datetime(2008, 6, 22, 0, 0), 22, 'B'),
(datetime.datetime(2008, 6, 22, 0, 0), 15, 'C'),
(datetime.datetime(2008, 6, 22, 0, 0), 16, 'A'),
(datetime.datetime(2008, 6, 23, 0, 0), 17, 'A'),
(datetime.datetime(2008, 6, 23, 0, 0), 0, 'B'),
(datetime.datetime(2008, 6, 23, 0, 0), 14, 'C')]

Resulted list required:
Dates=[datetime.datetime(2008, 6, 21, 0, 0),datetime.datetime(2008, 6, 22, 0, 0),datetime.datetime(2008, 6, 23, 0, 0)]
A=[26,16,17]
B=[0,22,0]
c=[0,15,14]

Here we are constructing list for DATES, A,B & C.As you can see in resulted list for A,B,C, it put 0 if there is no data for given date. I am not sure how I can achieve these resulted list for Dates,A,B,C.

Thanks,
SOLUTION
Avatar of mish33
mish33
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pepr
pepr

mish33: The line 7 have to be changed to C = []. Nice solution; however, the input_list must be sorted by the date. It is the case in the example. It depends how it is collected.

On the other hand, it may be rather difficult to understand to a beginner.

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial