【发布时间】:2017-01-10 16:44:44
【问题描述】:
我正在寻找一种简洁的 Pythonic 方法来检查列表的内容是否大于给定数量(第一阈值)一定次数(第二阈值)。如果两个语句都为真,我想返回超过给定阈值的第一个值的索引。
示例:
# Set first and second threshold
thr1 = 4
thr2 = 5
# Example 1: Both thresholds exceeded, looking for index (3)
list1 = [1, 1, 1, 5, 1, 6, 7, 3, 6, 8]
# Example 2: Only threshold 1 is exceeded, no index return needed
list2 = [1, 1, 6, 1, 1, 1, 2, 1, 1, 1]
【问题讨论】:
-
示例 1 未通过您的第二个阈值。正好有 5 个数字 >4
-
@Chris_Rands 我认为他的门槛是包容性的
-
我也注意到了,OP 交替使用 'exceeded' 和 'reached' 所以很难猜出他想要什么。
-
@Chris_Rands 阈值可以大于等于或大于,这并不重要,抱歉对这个事实含糊不清。明天将仔细查看代码答案!已经要感谢大家的精彩回答!
标签: python list iterator threshold