Relate index lists in python

0

Good afternoon!

I have the list:

a=[4,3, 1, 2, 3]

and the list:

sub_ind_novo=[96, 7, 97, 23, 18]

And I needed to list the lists getting the result: no97. G1 #When the% is 97 , the match in a is 1 , so just print 1 instead I need the element of sub_ind_new to be printed the number of times of the corresponding element in a. (from here, when it takes values different from 1 to a do not know how to do .. ie the 1st element in a is 4, and the one in sub_ind_new is 96, so should 4 lines of this value appear in the way I write here below in the result example) The remainder should be in this form (result example):

no23. G1
no23. G2
no7. G1
no7. G2
no7. G3
no18. G1
no18. G2
no18. G3
no96. G1
no96. G2
no96. G3
no96. G4

And I made the following code but I can not do it for the times where a is different from 1 , ie 2 , 3 , 4 or 5 ) ..

for i in range(0,len(a)):
    if a[i]==1:
        print ("no%d. G1 \n" %sub_ind_novo[i])

    elif:
        .....
        print ("no%d. G%d \n" %(sub_ind_novo[i],))

Thank you!

    
asked by anonymous 28.05.2016 / 19:18

2 answers

1

The problem can be solved with a few lines of code:

from operator import itemgetter

a = [4, 3, 1, 2, 3]                                            
sub_ind_novo = [96, 7, 97, 23, 18]

# relacionamos as listas e ordenamos de acordo com a lista a                                                           
relacao = sorted(list(zip(sub_ind_novo, a)), key=itemgetter(1))

for rel in relacao:                                            
    for i in range(1, rel[1] + 1):                             
        print('no{0}. G{1}'.format(rel[0], i))                                                                               

The result will be exactly what you need:

no97. G1
no23. G1
no23. G2
no7. G1 
no7. G2 
no7. G3 
no18. G1
no18. G2
no18. G3
no96. G1
no96. G2
no96. G3
no96. G4
    
20.01.2017 / 12:11
0

see if this can also be useful

a=[4,3, 1, 2, 3]
sub_ind_novo=[96, 7, 97, 23, 18]

for i in range(0,len(a)):
    if a[i]==1:
        print "no{}. G1 \n".format(sub_ind_novo[i])

    elif a[i] != 1:
        print "no{}. G{} \n".format(sub_ind_novo[i], a[i])

Output

no96. G4 

no7. G3 

no97. G1 

no23. G2 

no18. G3 

What was the solution that solved your question? if possible share knowledge with the crowd

    
28.05.2016 / 21:19