osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Enumerate - int object not subscriptable


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