Enumerate - int object not subscriptable
Ian Kelly
ian.g.kelly at gmail.com
Tue Aug 20 13:15:06 EDT 2019
Or use the "pairwise" recipe from the itertools docs:
from itertools import tee
def pairwise(iterable):
"s -> (s0,s1), (s1,s2), (s2, s3), ..."
a, b = tee(iterable)
next(b, None)
return zip(a, b)
for num1, num2 in pairwise(a):
print(num1, num2)
On Tue, Aug 20, 2019 at 7:42 AM Cousin Stanley <cousinstanley at gmail.com>
wrote:
> Sayth Renshaw wrote:
>
> > I want to do basic math with a list.
> >
> > a = [1, 2, 3, 4, 5, 6, 7, 8]
> >
> > for idx, num in enumerate(a):
> > print(idx, num)
> >
> > This works, but say I want to print the item value
> > at the next index as well as the current.
> >
> > for idx, num in enumerate(a):
> >
> > print(num[idx + 1], num)
> > ....
>
>
> #!/usr/bin/env python3
>
> # sum each adjacent pair of elements in a list
>
> ls = list( range( 10 , 1 , -1 ) )
>
> print('\n ' , ls , '\n' )
>
> for enum , n in enumerate( range( len( ls ) - 1 ) ) :
>
> i_left , i_rite = ls[ n : n + 2 ]
>
> i_tot = i_left + i_rite
>
> print( ' {:2d} : {:2d} + {:2d} = {:4d} '.format( enum , i_left ,
> i_rite , i_tot ) )
>
>
> --
> Stanley C. Kitching
> Human Being
> Phoenix, Arizona
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>
More information about the Python-list
mailing list