user102724
user102724

Reputation: 89

How to use counter in for loop python

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?

Upvotes: 1

Views: 8111

Answers (7)

bignose
bignose

Reputation: 32309

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')

Upvotes: 8

David Z
David Z

Reputation: 131550

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...

Upvotes: 4

Boris Gorelik
Boris Gorelik

Reputation: 31767

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]

Upvotes: 2

Agos
Agos

Reputation: 19440

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!

Upvotes: 0

Anurag Uniyal
Anurag Uniyal

Reputation: 88737

  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?

Upvotes: 2

unwind
unwind

Reputation: 399803

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.

Upvotes: 2

SpliFF
SpliFF

Reputation: 38966

counter += 1

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

Upvotes: 1

Related Questions