List indexes in python

Asked

Viewed 560 times

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 link the lists by getting the result: no97. G1 #when sub_ind_novo is 97, the corresponding in a is 1, so just print this 1 time I need the sub_ind_new element printed as the number of times of the corresponding element in a. (from here, when it takes values other than 1 in a I don’t know how to do.. that is, the 1st element in a is 4, and the respective sub_ind_new is 96, so it should appear 4 lines of this value in the way I write here below in the output example) The rest should be in this form (example of result):

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’t make it for the times when it’s different from 1, that is, where the (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!

  • What language is this that Voce speaks?

  • Voce could make a simpler analogy of your problem?

  • I’m working on python 2.7

  • You need to explain better. You want to know what the values of a which are equal to 1, and go to the respective index where you found them in sub_ind_novo? In this case he submitted he wants to withdraw the figures sub_ind_novo[11] and sub_ind_novo[7] ?

  • I’ve already edited, I’ve shortened the list size to make it easier. No, the "a" list is like a list of occurrences of the respective values of "sub_ind_new", so you should print the "sub_ind_new" element the number of times the respective element in "a", as in the result example. (96 has to appear 4 times, in the form: G1, G2, G3, G4; the 23 twice in the form: G1, G2, etc.. )

  • very confusing, puts a larger part of the code and tries to explain better your problem

  • I’ve already worked it out, thank you!

Show 2 more comments

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

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

  • I solved my question like this: for i in range(0,Len(a)): if a[i]=1: print ("no%d. G1 n" %sub_ind_new[i]) Else: for j in range(1,a[i]+1): print ("no%d. G%d n" %(sub_ind_new[i],j))

Browser other questions tagged

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