Averages of elements in several lists

0

I need to calculate the sum of several elements in different lists (from index 0).

lista1 = [154,12,998,147]
lista2 = [897,123,998,59877]
lista3 = [3,789,111,555,699]

This would be the simplistic way:

soma = lista1[0]+lista2[0]+lista3[0]

But I need to extract the values and apply the methods rolling().sum()

lista4 = pd.Series([154,897,3])
lista4.rolling(2).sum()

something like that !!

    
asked by anonymous 13.11.2018 / 20:27

1 answer

1

If you have a list of lists:

lista1 = [154,12,998,147]
lista2 = [897,123,998,59877]
lista3 = [3,789,111,555,699]

listas = [lista1, lista2, lista3]

You can create a list of the first elements by doing:

primeiros = [lista[0] for lista in listas]

Thus,% w / w of% will be the list [154, 897, 3].

As suggested in the comments, one possibility is to define a function that returns the values of a certain list index:

def get_on_index(*listas, index=0):
    return [lista[index] for lista in listas]

So, if you need all the values that are in index 2, just do:

valores = get_on_index(lista1, lista2, lista3, index=2)  # [998, 998, 555]
    
13.11.2018 / 20:43