Convert list to another form but providing same information

Steven D'Aprano steve at pearwood.info
Mon Mar 21 20:13:49 EDT 2016


On Tue, 22 Mar 2016 05:26 am, Maurice wrote:

> I have a list such [6,19,19,21,21,21] (FYI this is the item of a certain
> key in the dictionary)
> 
> And I need to convert it to a list of 32 elements (meaning days of the
> month however first element ie index 0 or day zero has no meaning -
> keeping like that for simplicity's sake). Therefore the resulting list
> should be:
> [0,0,0,0,0,0,1,0,0,0...,2,0,3,0...0]
> 
> So the list index should tell how many occurrences of a certain day in the
> original list.

the_list = [6,19,19,21,21,21]
days = [0]*32
for item in the_list:
    days[item] += 1



-- 
Steven




More information about the Python-list mailing list