1
my_date_list = ['01', '02', '03','04','05','06','07','08','09','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24','25','26','27','28','29','30','31']
str_date_list=[]
for item in my_date_list:
    str_date_list.append(item+'-'+'05' + '-' +'09')

counter= 0
i = iter(range(31))
for item in i:
    daily_user_status_list=[]
    print counter
    val_time1 = str_date_list[counter]
    val_time2 = str_date_list[counter + 1]
    counter =counter + 1

I am getting code error while doing counter = counter + 1. Basically, I need to different time from my str_date_list each time. but counter = counter +1 give me code error.

Is there any other way of doing it?

2
  • please consider removing the django and facebook tags Commented May 20, 2009 at 8:28
  • 1
    Please include the actual error message. Commented May 20, 2009 at 11:08

7 Answers 7

8

The counter is getting out of step with the sequences you're iterating over. But more than that, the counter is totally unnecessary.

You've got several manual iterations of things that could be automated, and they're causing you to trip over. Especially, you hardly ever need to manually track a counter while iterating; Python's sequence types know how to iterate themselves.

Here's my re-write of the intent of the above code (in the interactive interpreter to show it working):

>>> dates = ["%(day)02d-05-09" % vars() for day in range(1, 31+1)]
>>> date_ranges = zip(dates[:-1], dates[1:])
>>> for (date_begin, date_end) in date_ranges:
...     print (date_begin, date_end)
... 
('01-05-09', '02-05-09')
('02-05-09', '03-05-09')
('03-05-09', '04-05-09')
…
('28-05-09', '29-05-09')
('29-05-09', '30-05-09')
('30-05-09', '31-05-09')
Sign up to request clarification or add additional context in comments.

2 Comments

I see we were thinking the same thing at the same time ;-)
"%02d-05-09" % day instead of vars() variant might be more readable.
4

Just for kicks, here's the super-compact Pythonic way to write this:

from itertools import izip, islice
str_date_list = ['%02d-05-09' % i for i in xrange(1, 32)]
for val_time1, val_time2 in izip(islice(str_date_list, 0, None), islice(str_date_list, 1, None)):
    daily_user_status_list = [ <whatever goes here> ]
    # more code...

Comments

2

The error you're seeing is because you're indexing out of range on the str_date_list list, not because you're incrementing the variable.

Compare the largest value of counter that the loop prints (30) to the length of the list (len(str_date_list)). Since indexing starts at 0, the largest index into a list of length n is n - 1.

Comments

2
  1. you do not need to create a iterator for going thru 0-31 you can use enumerate e.g.

    for i, sdate in enumerate(str_date_list): print i, sdate

  2. if you are using iter isn't item and counter same?

Comments

2

You don't need to duplicate the loop iteration variable and the counter:

my_date_list = ['01', '02', '03','04','05','06','07','08','09','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24','25','26','27','28','29','30','31']
str_date_list=[]
for item in my_date_list:
    str_date_list.append(item+'-'+'05' + '-' +'09')

for i in xrange(len(my_date_list)-1):
    daily_user_status_list=[]
    print i
    val_time1 = str_date_list[i]
    val_time2 = str_date_list[i + 1]

Comments

1

counter += 1

but that isn't the problem. What's the error? Indentation error maybe?

Comments

0

Better written:

str_date_list=[]
for n in xrange(1,32):
    str_date_list.append(str(n).zfill(2)+'-'+'05' + '-' +'09')

for i in xrange(len(str_date_list)):
    daily_user_status_list=[]
    print i
    val_time1 = str_date_list[i]
    val_time2 = str_date_list[i + 1]
  • xrange gives us a (quite performing) iterator over natural numbers given bounds.
  • we use zfill to make sure there is a leading zero instead of writing them all explicitly
  • it's important to avoid iterating out of the array bounds!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.