osdir.com


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

Enumerate - int object not subscriptable


On Wednesday, 21 August 2019 03:16:01 UTC+10, Ian  wrote:
> 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)
> 

> > Stanley C. Kitching
> > Human Being
> > Phoenix, Arizona
> >
> > --

This definitely ended up being the ticket.

def pairwise(iterable): 
    "s -> (s0,s1), (s1,s2), (s2, s3), ..." 
    a, b = tee(iterable) 
    next(b, None) 
    return zip(a, b) 


def output_data(s):
    serie = fibo(input_length)
    x = []
    y = []
    
    for num1, num2 in pairwise(serie): 
        y.append( num2 / num1) 

    for item in y:
        x.append(y.index(item))

    draw_graph(x, y)

########
Kept trying with the enumerate style
for i, num in enumerate(serie):
    while i < len(serie)-1:
    x.append(int(serie[i + 1])/ int(serie[i]))
    i += 1

but just couldn't quite get it to work. Itertools definitely made it easier.

for i, num in enumerate(serie):
    while i < len(serie)-1:
    x.append(int(serie[i + 1])/ int(serie[i]))
    i += 1