Lists and dictionaries python

5

I'm working on Python 2.7.

import collections
counter_ger=collections.Counter(sub_todaas)
a=counter_ger.values()
b=counter_ger.keys()
sub_ind_novo = [Ntodos.index(i) for i in b if i in Ntodos]

(where Nodes contains many names, among which are the ones that make up the "b" list and this part was to know their index when they occur in "All")

#counter_ger=Counter({u'Fe60': 12, u'Falag60': 8, u'Pe60': 6, u'Chaf60': 4, u'Par': 4, u'Vila': 4, u'Per60': 4, u'Cast60': 4, u'Fala150': 3, u'Tab60': 3, u'Zez': 3, u'Pe220': 2, u'Chaf220': 1, u'Fe220': 1})
#a=[4, 8, 3, 4, 4, 12, 4, 1, 2, 3, 3, 1, 6, 4]
#b=[u'Chaf60', u'Falaga60', u'Fal150', u'Par', u'Vila', u'Fe60', u'Pe60', u'Chaf220', u'Pe220', u'Tab60', u'Zez', u'Fe220', u'Pe60', u'Cast60']
#sub_ind_novo=[96, 101, 7, 42, 39, 99, 97, 23, 18, 94, 67, 68, 95, 100]

To relate a list p_prod, with the previous ones, having the values of b in common I used this:

def indicesDeElementoNaLista(elementoProcurado, lista):
    return [i for (i, elemento) in enumerate(lista) if elemento == elementoProcurado]

def elementosNasPosicoes(lista, posicoes):
    return [lista[i] for i in posicoes]

prod_barr = {}
elementosCorrespondentes=[]
for elemento in b:
    posicoes = indicesDeElementoNaLista(elemento, sub_todaas)
    elementosCorrespondentes.append(elementosNasPosicoes(p_prod, posicoes))
    prod_barr[elemento] = elementosCorrespondentes
Lista=[]
for i in elementosCorrespondentes:
    for j in i:
        Lista.append(j)
#Lista=[4.223212, 35.955, 28.54111, 6.69015, 2.004056, 9.731978999999999, 3.508121, 0.7185728, 27.90795, 0.31004020000000004, 4.372325, 0.8837922, 99.0, 116.0, 36.0, 1.066429, 4.588346, 53.5449, 0.6904233000000001, 11.350000381469727, 17.450000762939453, 11.0, 20.1200008392334, 1.711943, 3.012495, 0.2186731, 4.320139, 12.5081, 0.3079727, 22.61825, 16.73781, 1.014131, 26.747880000000002, 0.019620460000000003, 0.6058, 11.27064, 14.42632, 1.147464, 4.8489889999999995, 38.0, 229.0, 20.639999389648438, 5.87853, 32.93286, 73.79299, 60.0, 50.0, 105.0, 20.979999542236328, 0.8396113000000001, 12.70308, 1.2727460000000002, 3.1277649999999997, 0.1861473, 6.1493329999999995, 21.105880000000003, 4.862599, 91.31339999999999, 1.014135]

I needed to have the result in the form: No. 96. G1 = 4.223212 No. 96. G2 = 35.955 No. 96. G3 = 28.54111 ... no69 G1 = 0.08837922 no95. G1 = 99.000000 no95. G2 = 116

So I did:

for i in range(0,len(a)):

    if a[i]==1:
        print ("no%d. G1 = %f \n" %(sub_ind_novo[i],Lista[i]))

    else:
        for j in range(1,a[i]+1):
            print ("no%d. G%d = %f \n" %(sub_ind_novo[i],j,Lista[i]))

The values of no and G give well, but I do not know how to go through the list "List" there .. In fact the cycle goes from 0 to 14 which is the size of "a" and "List" has 59 elements. But as "a" is a list of occurrences, when G1, G2, G3, G4 appears, until the number of that occurrence (elemntos of "a" at each iteration) will make the 59 at the end! Is there any way to do this?

    
asked by anonymous 30.05.2016 / 18:58

1 answer

1

From what I realized it could be this, see if it helps

count = 0;
for i in range(0, len(a)):
    for j in range(0,a[i]):
        if a[j]==1:
            print ("no%d. G1 = %f \n" %(sub_ind_novo[i],Lista[count]))
        else:
            print ("no%d. G%d = %f \n" %(sub_ind_novo[i],j+1,Lista[count]))
        count += 1

That is, we will go through a and within each iteration we will a[i] iterations, ie in the first we will do 4 in the second we will do 8 etc ... In all we will do 59 ( count ), which fits well with the number of all items in Lista

    
30.05.2016 / 19:48