📜  Python|阈值大小 更大的字符串频率

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

Python|阈值大小 更大的字符串频率

有时,在处理大量数据时,我们可能会遇到一个问题,即我们需要知道大于特定长度的特定大小字符串的数量。在跨多个域的验证案例期间可能会出现此类问题。让我们讨论在Python字符串列表中处理此问题的某些方法。

方法 #1:使用列表理解 + len()
上述功能的组合可用于执行此任务。在此,我们迭代所有字符串并仅返回长度大于 K 使用 len() 检查的字符串。使用 len() 提取计数。

Python3
# Python3 code to demonstrate working of
# Threshold Size Greater Strings Frequency
# using list comprehension + len()
 
# initialize list
test_list = ['gfg', 'is', 'best', 'for', 'geeks']
 
# printing original list
print("The original list : " + str(test_list))
 
# initialize K
K = 3
 
# Threshold Size Greater Strings Frequency
# using list comprehension + len()
res = len([ele for ele in test_list if len(ele) >= K])
 
# printing result
print("The frequency of threshold K sized strings are : " + str(res))


Python3
# Python3 code to demonstrate working of
# Threshold Size Greater Strings Frequency
# using filter() + lambda + len()
 
# initialize list
test_list = ['gfg', 'is', 'best', 'for', 'geeks']
 
# printing original list
print("The original list : " + str(test_list))
 
# initialize K
K = 3
 
# Threshold Size Greater Strings Frequency
# using filter() + lambda + len()
res = len(list(filter(lambda ele: len(ele) >= K, test_list)))
 
# printing result
print("The frequency of threshold K sized strings are : " + str(res))


输出
The original list : ['gfg', 'is', 'best', 'for', 'geeks']
The frequency of threshold K sized strings are : 4


方法 #2:使用 filter() + len() + lambda
上述功能的组合可用于执行此任务。在此,我们使用 filter() 提取元素,并在 lambda函数中编译逻辑。使用 len() 提取计数。

Python3

# Python3 code to demonstrate working of
# Threshold Size Greater Strings Frequency
# using filter() + lambda + len()
 
# initialize list
test_list = ['gfg', 'is', 'best', 'for', 'geeks']
 
# printing original list
print("The original list : " + str(test_list))
 
# initialize K
K = 3
 
# Threshold Size Greater Strings Frequency
# using filter() + lambda + len()
res = len(list(filter(lambda ele: len(ele) >= K, test_list)))
 
# printing result
print("The frequency of threshold K sized strings are : " + str(res))
输出
The original list : ['gfg', 'is', 'best', 'for', 'geeks']
The frequency of threshold K sized strings are : 4