[Tutor] print date and skip any repeats

questions anon questions.anon at gmail.com
Tue Sep 23 07:08:52 CEST 2014


Thank you for your help.
Using your feedback and adding the input dates and the output dates into
lists.
It seems to work although I am not sure if this is the
correct/fastest/pythonic way to go about it. As I said it works... I have
pasted below if anyone else needs something similar
thanks for your help

lastdate=all_the_dates[1]

onedateperday.append(lastdate)

print onedateperday

for date in all_the_dates:

    if date !=lastdate:

        lastdate=date

        onedateperday.append(lastdate)


On Wed, Sep 17, 2014 at 7:35 PM, Cameron Simpson <cs at zip.com.au> wrote:

> 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>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20140923/84fefe0f/attachment.html>


More information about the Tutor mailing list