Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help appreciated
#9
(Aug-22-2019, 08:33 AM)Malt Wrote: I'm not understanding this part.. how this min works here? say for iteration 1, it will do like min([110,201,305],key=10).. is my understanding right. Please clarify me and why we need key here? I mess up Sick

Also, how 201 is min when 110 is already there? I feel I'm over looking

It's always good to start with built-in help:

>>> help(min)
Help on built-in function min in module builtins:

min(...)
    min(iterable, *[, default=obj, key=func]) -> value
    min(arg1, arg2, *args, *[, key=func]) -> value
    
    With a single iterable argument, return its smallest item. The
    default keyword-only argument specifies an object to return if
    the provided iterable is empty.
    With two or more arguments, return the smallest argument.
(END)
Not the easiest read, but one can observe key=func

In simpler terms it can be described as:

Quote:For complex processing, min() can be called with an optional "key=" function that transforms each element before comparison. The key function takes in 1 value and returns 1 value, and the returned "proxy" value is used for finding minimum value.

What happens under the hood? Exactly this: every item in iterable is processed by key function and returned value is used to find minimum value.

It can be illustrated by this example:

>>> nums = [110, 201, 305]
>>> [num % 100 for num in num]       # find reminders
[10, 1, 5]
>>> min(num % 100 for num in nums)   # find smallest reminder
1
Python does it more efficiently but as general idea one can describe principle this way:

>>> nums = [110, 201, 305]
>>> reminders = [num % 100 for num in nums]   
>>> i = reminders.index(min(reminders)) # find index of item with minimum value in reminders
>>> nums[i]                             # find corresponding value in nums
201
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Messages In This Thread
Help appreciated - by elperex - Aug-20-2019, 11:52 AM
RE: Help appreciated - by wavic - Aug-20-2019, 03:03 PM
RE: Help appreciated - by perfringo - Aug-21-2019, 07:22 AM
RE: Help appreciated - by millpond - Aug-22-2019, 04:31 AM
RE: Help appreciated - by Malt - Aug-22-2019, 05:24 AM
RE: Help appreciated - by ThomasL - Aug-22-2019, 05:28 AM
RE: Help appreciated - by perfringo - Aug-22-2019, 06:15 AM
RE: Help appreciated - by Malt - Aug-22-2019, 08:33 AM
RE: Help appreciated - by perfringo - Aug-22-2019, 09:50 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Any help would be appreciated tegtheWegg 1 1,743 Feb-10-2021, 12:52 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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