Python Forum
Replace elements of array with elements from another array based on a third array
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Replace elements of array with elements from another array based on a third array
#5
It seems you are presenting a part of a mysterious algorithm. Smile
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]
Reply


Messages In This Thread
RE: Replace elements of array with elements from another array based on a third array - by ibreeden - May-13-2022, 04:56 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Writing a cycle to find the nearest point from the array Tysrusko 0 130 May-10-2024, 11:49 AM
Last Post: Tysrusko
  Good way to ignore case when searching elements? Winfried 1 236 Apr-25-2024, 12:39 PM
Last Post: menator01
  Elegant way to apply each element of an array to a dataframe? sawtooth500 7 497 Mar-29-2024, 05:51 PM
Last Post: deanhystad
  Concatenate array for 3D plotting armanditod 1 313 Mar-21-2024, 08:08 PM
Last Post: deanhystad
  Convert numpy array to image without loading it into RAM. DreamingInsanity 7 5,968 Feb-08-2024, 09:38 AM
Last Post: paul18fr
  unable to remove all elements from list based on a condition sg_python 3 505 Jan-27-2024, 04:03 PM
Last Post: deanhystad
  How Write Part of a Binary Array? Assembler 1 414 Jan-14-2024, 11:35 PM
Last Post: Gribouillis
  Loop over an an array of array Chendipeter 1 615 Nov-28-2023, 06:37 PM
Last Post: deanhystad
  How to remove some elements from an array in python? gohanhango 9 1,370 Nov-28-2023, 08:35 AM
Last Post: Gribouillis
  IPython errors for numpy array min/max methods muelaner 1 612 Nov-04-2023, 09:22 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020