Receive 12 elements and separate them into 3 lists

Asked

Viewed 54 times

2

I need to receive three lists generated in the shell to process in Python:

l1=4 3 2 1
l2=2 1 3 4
l3=1 2 3 4

In the shell script step this way:

python3 orth_median.py ${o_l1[*]} ${o_l2[*]} ${o_l3[*]}

Python takes 12 numbers.

In python I had to convert the 12 numbers into 3 lists to have the 3 lists again. Is this the best way or is there another way to do it? I am beginner working with shell script.

if __name__ == "__main__":
    if len(sys.argv)>11:
        size1=len(sys.argv[1:])/3 
        l1=[sys.argv[1:][0],sys.argv[1:][1],sys.argv[1:][2],sys.argv[1:][3]]
        l2=[sys.argv[1:][4],sys.argv[1:][5],sys.argv[1:][6],sys.argv[1:][7]]
        l3=[sys.argv[1:][8],sys.argv[1:][9],sys.argv[1:][10],sys.argv[1:][11]]
        A=np.array(generate_matrix(l1, int(size1)))
        B=np.array(generate_matrix(l2, int(size1)))
        C=np.array(generate_matrix(l3, int(size1)))
        M = orth_median1(A, B, C)
        print('o_m', M)
    else:
        print('No arguments provided.') 

1 answer

1


You can use the syntax of slicing to take a specific excerpt from sys.argv - remembering that the first element of sys.argv (at zero position) is the name of the script, so the numbers will be at positions 1 to 12 (ie you should check if it has at least 13 elements):

if len(sys.argv) >= 13:
    l1 = sys.argv[1:5]
    l2 = sys.argv[5:9]
    l3 = sys.argv[9:13]

Thus, l1 shall have the elements of indices 1 to 4 (the final value is not included), l2 shall be indexed 5 to 8 and l3 of indices 9 to 12.


Another option to do the same thing:

l1, l2, l3 = [ sys.argv[n:n + 4] for n in range(1, 13, 4) ]

I use a range from 1 to 12, jumping from 4 to 4 (i.e., numbers 1, 5 and 9), and for each of these numbers, I take a Slice from number to 4 positions after. I also use a comprehensilist on, which creates a list of the 3 sub-lists, and I do the multiple assignment, which assigns these sub-lists directly to l1, l2 and l3.

With this the lists will be respectively ['4', '3', '2', '1'], ['2', '1', '3', '4'] and ['1', '2', '3', '4'].


Not directly related, but to calculate the size1, you can use the entire split operator //, that already returns an integer value (so you don’t need to call int after). And how you are using sys.argv[1:] (that is, ignoring the first element of sys.argv), it is simpler to subtract 1 of the size, instead of creating another sub-list just for that.

And it is also possible to create arrays already A, B and C at once (if not using the lists l1, l2 and l3 for nothing else):

size1 = (len(sys.argv) - 1) // 3

# criar os arrays de uma vez
A, B, C = [ np.array(generate_matrix(sys.argv[n:n + 4], size1)) for n in range(1, 13, 4) ]
  • Your answer shows me that I need to learn more. Thank you! In the last line of code you posted you feiz two tasks in a row sounds. how could put in two lines of code?

  • @shermila Would that be: https://repl.it/repls/TiredColossalQuarks#main.py ? Something else: If the answer solved your problem, you can accept it, see here how and why to do it. It is not mandatory, but it is a good practice of the site, to indicate to future visitors that it solved the problem. Don’t forget that you can also vote in response, if it has found it useful.

Browser other questions tagged

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