此代码如何从整数字符串中提取最大值和最小值?

How does this code extract the max and min from a string of integers?

这是我在 codewars.com 上找到的解决方案 this kata

def high_and_low(numbers):
  return " ".join(x(numbers.split(), key=int) for x in (max, min))

我不明白这个解决方案是如何工作的,x函数看起来像一个排序函数,所以OP怎么没有使用排序?另外,元组 (max,min) 上的 for 循环如何提取最大值和最小值?我迷路了。也感谢与此相关的任何 examples/links。提前感谢您的努力。

根据 max 的文档(以及类似的 min),key=int 用于对可迭代对象进行排序,在本例中是执行 [=12 后获得的字符串列表=].然后 max 和 min 将从列表中选择最大和最小整数

for x in (max, min) 本质上用函数 maxmin

替换 x

一个更简单的方法可能是先提取整数列表,然后对其应用最大值和最小值

def high_and_low(numbers):

  nums = list(map(int, numbers.split()))
  return min(nums), max(nums)