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 a code error while executing counter = counter + 1

. Basically, I need a different time from mine str_date_list

every time. but counter = counter +1

give me a code error.

Is there any other way to do this?

+1


a source to share


7 replies


The counter breaks down with the sequences you repeat. Moreover, the counter is absolutely unnecessary.

You have a few manual iterations of things that can be automated, and they keep you traveling. In particular, you hardly ever need to manually keep track of the counter during iteration; Python sequence types know how to iterate themselves.



Here's my re-entry on the intent of the above code (in an interactive interpreter to show it works):

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

      

+8


a source


Just for kicks, here's a 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...

      

+4


a source


The error you are seeing is due to the fact that you are indexing out of range in the list str_date_list

, not because you are incrementing that variable.

Compare the largest value counter

that the loop prints ( 30

) along the length of the list ( len(str_date_list)

). Since indexing starts at 0

, the largest index in the length list n is n - 1

.

+2


a source


You don't need to duplicate the loop iteration variable and 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]

      

+2


a source


  • you don't need to create an iterator to go through 0-31 you can use an enum for example.

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

  • If you are using iter, is it not an item and a counter?

+2


a source


counter + = 1

But it's not a problem. What mistake? Perhaps an indented error?

+1


a source


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 (perfectly executable) iterator over natural numbers given by limits.
  • we use zfill to make sure there is the first zero instead of writing everything explicitly
  • It is important to avoid repeating array boundaries!
0


a source







All Articles