May-13-2022, 04:56 PM
It seems you are presenting a part of a mysterious algorithm.
You can do it the way you showed, but of course you must extend the "if" with a number of "elifs".
If I understand it correctly, the first array (input) and the third (control) must have the same number of items. Am I right?
The following construction is not easy to digest for a computer:
You can do it the way you showed, but of course you must extend the "if" with a number of "elifs".
If I understand it correctly, the first array (input) and the third (control) must have the same number of items. Am I right?
The following construction is not easy to digest for a computer:
array(['No change', 'Change second element to 1', 'Change first element to 1', 'Change fourth element to 1'], dtype='<U27')I hope you can change this to something more suitable like I did in the following example.
input_list = [[0.05, 0.07 , 0.04, 0.03, 0.05, 0.72], [0.08, 0.083, 0.40 , 0.038, 0.05, 0.70], [0.40, 0.17, 0.07, 0.04, 0.03, 0.31], [0.16, 0.14, 0.08, 0.40, 0.08, 0.40]] replacement = [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]] # array(['No change', 'Change second element to 1', # 'Change first element to 1', 'Change fourth element to 1'], # dtype='<U27') # This array has to be coded in a useful manner. (What is dtype='<U27'? I ignore it.) # I call it "control" because it controls the way "input_list" is # converted to "target". # A value of "-1" means: 'No change'. # Elements count from 0. So first element is 0, second element is 1 etc. # Then the previous array looks like this: control = [-1, 1, 0, 3] target = [] if len(input_list) != len(control): raise Exception("ERROR: input_list does not have the same number of elements as control list.") for i in range(len(input_list)): if control[i] == -1 : target.append(input_list[i]) else: target.append(replacement[control[i]]) # Show result. for item in target: print(item)
Output:[0.05, 0.07, 0.04, 0.03, 0.05, 0.72]
[0, 1, 0, 0]
[1, 0, 0, 0]
[0, 0, 0, 1]