Python Forum
'Get closest value array in array of arrays.' follow up help.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
'Get closest value array in array of arrays.' follow up help.
#3
Thanks for the reply - the way you have done it is quite intriguing.
I sort of had a similar idea, but it was very fragmented.
This is as far as I got in the time I had:
def find_closest2(col, array2d):
  r,g,b = col[0], col[1], col[2]
  first_el = [i[0] for i in array2d]
  second_el = [i[1] for i in array2d]
  third_el = [i[2] for i in array2d]
which is almost exactly what you did in this line:
for num, elm in enumerate(array2d):
.
Dream

I've worked some magic and compressed the function to a single line:
def find_closest2(col, array2d):
    get_lowest = lambda rgb_vals: min([[abs(col[0]-elm[0]) + abs(col[1]-elm[1]) + abs(col[2]-elm[2]), num] for num, elm in enumerate(rgb_vals)], key=lambda x: x[0])[1]
    return array2d[get_lowest(array2d)]
It is difficult to tell if it is working, however, it doesn't produce errors so that's a good sign.
I am going to test it out in my full project because that will really make sure it is working correctly.
Reply


Messages In This Thread
RE: 'Get closest value array in array of arrays.' follow up help. - by DreamingInsanity - Dec-01-2019, 02:10 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Writing a cycle to find the nearest point from the array Tysrusko 0 187 May-10-2024, 11:49 AM
Last Post: Tysrusko
  Elegant way to apply each element of an array to a dataframe? sawtooth500 7 525 Mar-29-2024, 05:51 PM
Last Post: deanhystad
  Concatenate array for 3D plotting armanditod 1 316 Mar-21-2024, 08:08 PM
Last Post: deanhystad
  Convert numpy array to image without loading it into RAM. DreamingInsanity 7 6,016 Feb-08-2024, 09:38 AM
Last Post: paul18fr
  How Write Part of a Binary Array? Assembler 1 424 Jan-14-2024, 11:35 PM
Last Post: Gribouillis
  Loop over an an array of array Chendipeter 1 623 Nov-28-2023, 06:37 PM
Last Post: deanhystad
  How to remove some elements from an array in python? gohanhango 9 1,403 Nov-28-2023, 08:35 AM
Last Post: Gribouillis
  IPython errors for numpy array min/max methods muelaner 1 622 Nov-04-2023, 09:22 PM
Last Post: snippsat
  Convert np Array A to networkx G IanAnderson 2 725 Jul-05-2023, 11:42 AM
Last Post: IanAnderson
  Help using a dynamic array excel formula with XLWings FXMonkey 2 1,359 Jun-06-2023, 09:46 PM
Last Post: FXMonkey

Forum Jump:

User Panel Messages

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