📌  相关文章
📜  Python|列表中大于 K 的值的数量

📅  最后修改于: 2022-05-13 01:55:35.895000             🧑  作者: Mango

Python|列表中大于 K 的值的数量

一个基本上是许多复杂问题的子问题的问题,即在Python的列表中查找大于某个数字的数字,是常见的问题,这篇特定的文章讨论了这个特定问题的可能解决方案。

方法一:朴素的方法
解决此问题的最常见方法是使用循环并仅计算大于给定数字 K 的元素的出现次数。

# Python 3 code to demonstrate 
# find number of elements > k
# using naive method 
  
# initializing list
test_list = [1, 7, 5, 6, 3, 8]
  
# initializing k
k = 4
  
# printing list 
print ("The list : " + str(test_list))
  
# using naive method 
# to get numbers > k
count = 0
for i in test_list :
    if i > k :
        count = count + 1
  
# printing the intersection 
print ("The numbers greater than 4 : " + str(count))

输出 :

The list : [1, 7, 5, 6, 3, 8]
The numbers greater than 4 : 4

方法 2:使用列表推导
此方法以类似的方式完成此任务,但方式更简洁。即使在后台运行类似的方法,列表理解总是会降低程序中的代码行数。

# Python 3 code to demonstrate 
# find number of elements > k
# using list comprehension
  
# initializing list
test_list = [1, 7, 5, 6, 3, 8]
  
# initializing k
k = 4
  
# printing list 
print ("The list : " + str(test_list))
  
# using list comprehension
# to get numbers > k
count = len([i for i in test_list if i > k])
  
# printing the intersection 
print ("The numbers greater than 4 : " + str(count))

输出 :

The list : [1, 7, 5, 6, 3, 8]
The numbers greater than 4 : 4

方法 3:使用sum()
sum()也可以帮助我们完成这个任务。当找到大于 k 的数时,我们可以返回 1,然后使用sum()计算总和

# Python 3 code to demonstrate 
# find number of elements > k
# using sum()
  
# initializing list
test_list = [1, 7, 5, 6, 3, 8]
  
# initializing k
k = 4
  
# printing list 
print ("The list : " + str(test_list))
  
# using sum()
# to get numbers > k
count = sum(i > k for i in test_list)
  
# printing the intersection 
print ("The numbers greater than 4 : " + str(count))

输出 :

The list : [1, 7, 5, 6, 3, 8]
The numbers greater than 4 : 4

方法 4:使用functools.reduce()
通过使用reduce() ,我们还可以对函数收集的所有数字进行求和,然后将它们累加以返回结果,即大于 K 的数字的计数。

# Python 3 code to demonstrate 
# find number of elements > k
# using reduce()
from functools import reduce
  
# initializing list
test_list = [1, 7, 5, 6, 3, 8]
  
# initializing k
k = 4
  
# printing list 
print ("The list : " + str(test_list))
  
# using reduce()
# to get numbers > k
count = reduce(lambda sum, j: sum  + (1 if j > k else 0), test_list, 0)
  
# printing the intersection 
print ("The numbers greater than 4 : " + str(count))

输出 :

The list : [1, 7, 5, 6, 3, 8]
The numbers greater than 4 : 4

方法 5:使用bisect() + sort()
sort()bisect()的组合实际上可以执行二进制搜索的任务,因此获取索引并减去列表的大小实际上可以帮助我们获得大于列表中特定元素的元素。

# Python 3 code to demonstrate 
# find number of elements > k
# using bisect() + sort()
from bisect import bisect
  
# initializing list
test_list = [1, 7, 5, 6, 3, 8]
  
# initializing k
k = 4
  
# printing list 
print ("The list : " + str(test_list))
  
# using bisect() + sort()
# to get numbers > k
test_list.sort()
count = len(test_list) - bisect(test_list, k)
  
# printing the intersection 
print ("The numbers greater than 4 : " + str(count))

输出 :

The list : [1, 7, 5, 6, 3, 8]
The numbers greater than 4 : 4