Python–因子计数小于 K 的元素

原文:https://www . geesforgeks . org/python-elements-with-factors-count-小于-k/

给定一个元素列表,获取因子小于 k 的所有元素

示例:

输入 : test_list = [60,12,100,17,18,19]。K = 4 输出:【17,19】 说明:两个元素都有 2 个因素。

输入 : test_list = [60,12,100,360,18,900]。K = 4 输出 : [] 说明:均有大于 4 的因子。

方法一:使用 列表理解

在这种情况下,我们使用列表理解来获得因子计数,而其他列表理解来迭代列表中的所有元素。

Python 3

# Python3 code to demonstrate working of
# Elements with factors less than K
# Using list comprehension

def factors_less_k(ele, K):

    # comparing for factors
    return len([idx for idx in range(1, ele + 1) if ele % idx == 0]) <= K

# initializing list
test_list = [60, 12, 100, 17, 18]

# printing original list
print("The original list is : " + str(test_list))

# initializing K
K = 4

# checking for each element
res = [ele for ele in test_list if factors_less_k(ele, K)]

# printing result
print("Filtered elements : " + str(res))

Output

The original list is : [60, 12, 100, 17, 18]
Filtered elements : [17]

方法 2:使用 滤镜()+lambda+len()+列表理解

在这种情况下,使用 filter()和 lambda,len()完成过滤任务,使用列表理解来执行获取因子的任务。

Python 3

# Python3 code to demonstrate working of
# Elements with factors less than K
# Using filter() + lambda + len() + list comprehension

def factors_less_k(ele, K):

    # comparing for factors
    return len([idx for idx in range(1, ele + 1) if ele % idx == 0]) <= K

# initializing list
test_list = [60, 12, 100, 17, 18]

# printing original list
print("The original list is : " + str(test_list))

# initializing K
K = 4

# filtering using filter() + lambda
res = list(filter(lambda ele: factors_less_k(ele, K), test_list))

# printing result
print("Filtered elements : " + str(res))

Output

The original list is : [60, 12, 100, 17, 18]
Filtered elements : [17]