python-3.x - python :find a number in a list (smaller but the biggest one)

标签 python-3.x algorithm list

假设我们使用的是 python: 如果给出一个整数 a 和一个整数列表。我们应该找到这个数字: 它应该小于a; 它是所有小于a的数字中最大的一个;

例如。 a=3, list=[1,2,3,4],这个数字应该是2

如果没有任何外部包,我应该如何到达这里?

最佳答案

假设列表存储在l = [1,2,3,4,5], filter内置函数可以为您提供条件为真的元素:

l = [1,2,3,4,5]
a = 3
f = filter(lambda x: x < a, l) #This stores elements that are smaller than a in a filter object
num = max(f) # filter is an iterable and max() built-in is difined on it
# num == 2

关于python-3.x - python :find a number in a list (smaller but the biggest one),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53809981/

相关文章:

python - 文本:更改值后 UI 未更新

java - 带数字的后缀数组/后缀树

algorithm - 高效准确地计算hypot(a,b) 的倒数

list - Haskell函数交换列表中的第二个元素

python - 如何将嵌套循环转换为 python 中的列表理解

python - 如何使用python中的行号从文本文件中删除一行

python - Standard scaler 和 MinMaxScaler 之间的区别

python - Tkinter:标签和按钮框架之间的空间太大

algorithm - 重新平衡衣物的最少轮次

结合范围和数字列表的python