[Tutor] print date and skip any repeats

Dave Angel davea at davea.name
Tue Sep 23 09:05:28 CEST 2014


Please post in text mode, not html.

questions anon <questions.anon at gmail.com> Wrote in message:
>
> lastdate=all_the_dates[1]
> onedateperday.append(lastdate)
> print onedateperday
> for date in all_the_dates:
>      if date !=lastdate:
>           lastdate=date
>          onedateperday.append(lastdate)

There are a number of things you don't explicitly specify.  But if
 you require the dates in the output list to be in the same order
 as the original list, you have a bug in using element [1]. You
 should be using element zero, or more simply:

lastdate=None
for date in all_the_dates:
    if date !=lastdate:
        onedateperday.append(date)
        lastdate=date

But if you specified a bit more, even simpler answers are possible. 

For example,  if output order doesn't matter, try:
     onedateperday = list (set (all_the_dates))

If output order matters, but the desired order is sorted,
    onedateperday = sorted ( list (set (all_the_dates)))

Other approaches are possible using library functions,  but this
 should be enough.



-- 
DaveA



More information about the Tutor mailing list