[Tutor] print date and skip any repeats
Cameron Simpson
cs at zip.com.au
Wed Sep 17 11:35:05 CEST 2014
On 17Sep2014 19:21, questions anon <questions.anon at gmail.com> wrote:
>I think this should be simple but I can't find the right commands.
>
>I have a date for each hour for a whole month (and more) and I would like to
>write a loop that prints each date that is different but skips the dates that
>are the same.
>
>for i in date:
>print i and then skip i until different
>print next i and then skip i until different
>etc.
If the above is what you're thinking, your problem seems to be that you're
putting the "skip i until different" inside the loop as though you do lots of
things in each loop iteration.
What you'd really be doing is _one_ thing inside each loop iteration: printing
the date or not. The normal way to do this is to keep a variable for the last
date _print_. Each loop iteration should compare the current date against that
and either do nothing or [print the new date and update the variable].
So you loop looks like this:
lastdate = ""
for date in all_the_dates:
if date != lastdate:
... a different date: print and then update last date ...
See if adapting that gets you closer to working code. If your code doesn't
work, post the actualy code and a description of what it seems to be doing
wrong.
Cheers,
Cameron Simpson <cs at zip.com.au>
Trust the computer industry to shorten Year 2000 to Y2K. It was this
thinking that caused the problem in the first place.
- Mark Ovens <marko at uk.radan.com>
More information about the Tutor
mailing list