Python lists and dictionaries

Asked

Viewed 480 times

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 Ntodos contains many names, among which are those that make up the list "b" and this part was to know their information when they occur in "Ntodos")

#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 p_prod list to 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 shape: paragraph 96. G1 = 4.223212 no96. G2 = 35.955 no96. 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 are good, but I don’t 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 it appears: G1, G2, G3, G4, until the number of this occurrence (elemntos of "a" at each iteration) will do the 59 at the end! Is there any way to do this?

  • tell me one thing... what relates List to the other lists we have. For example the element in a 15 of List, what has to do with the list a

  • For example the first 4 values of "List", are the values of G1, G2, G3, G4 for the 1st iteration of for, where a!=1 and a=4. That is, Chaf60 in "b", appears 4 times in "sub_todaas" (list I do not have here but I think should be irrelevant) and corresponds to this 96 in "sub_ind_novo". Thus it should appear: no96.G1 = 4.223212 no96.G2 = 35.955 no96.G3 = 28.54111 no96.G4 = 6.69015 then no101.G1 = 2.004056 ... but I have to be aware of the condition when a[i]=1, where it can only do G1 and has to resume the order of "List"

  • I think I got it... give me a minute

  • Why only the first 4? what is the condition to move to the next value of sub_ind_novo?

  • These 4 are the ones that belong to the same key as the counter_ger dictionary, which in this case is "Chaf60"

  • I’m here trying to help but I feel like I have very little data. I’m trying

  • How do I know it’s to move to the Prox of sub_ind_novo

  • if it takes a few more, some list or something I can send, I just avoid sending because they are usually extensive and then ask to summarize and simplify the questions.. but I can! Thank you!

  • No need here. But you can put it on google drive or http://www.filedropper.com/ and I’ll take a look at it

  • sub_ind_new is the list that contains the indices in which the names that appear in b, appear in a list of many names (Ntodos) and served only to fill in the "no" part and agree with the other code you already had. That helps?

Show 5 more comments

1 answer

1


From what I understand it might 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

I mean, let’s go a and within each iteration we go a[i] iterations, ie in the first we will make 4 on Monday we’ll do 8 etc... In all we will do 59 (count), what fits well with the number of all items in Lista

  • Give it right, thank you so much for the help!!! @Miguel

  • Anything at all @Sofia.

  • I think we made a mistake @Sofia, I think the condition you want is a[i]==1 and not a[j]==1, see how it is

  • Thanks @Miguel for the repair, I hadn’t seen it, but I found another problem and changed, to do what I wanted it to be like this: Count = 0; for i in range(0, Len(a): for j in range(1,a[i]+1): if a[i]=1: print ("no%d. G1 = %f n" %(sub_ind_new[i],List[Count])) Else: print ("no%d. G%d = %f n" %(sub_ind_new[i],j,List[Count]) Count += 1

  • And did it right now? But @Sofiaraimundo, for piece of code you will not use the j, I guess you can just for j in range(a[i]): that will make the same number of loops as range(1,a[i]+1)

  • I haven’t tried it, but it makes sense, I’ll try it! @Miguel

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.