📜  Python示例中的 Lambda 和过滤器

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

Python示例中的 Lambda 和过滤器

先决条件: Python中的 Lambda

给定一个数字列表,找出所有能被 13 整除的数字。

Input : my_list = [12, 65, 54, 39, 102, 
                     339, 221, 50, 70]
Output : [65, 39, 221]

我们可以在 filter() 内置函数中使用 Lambda函数来查找列表中所有可被 13 整除的数字。在Python中,匿名函数意味着函数没有名称。

Python中的 filter()函数接受一个函数和一个列表作为参数。这提供了一种优雅的方式来过滤掉序列“sequence”的所有元素,函数返回True。

# Python Program to find numbers divisible 
# by thirteen from a list using anonymous 
# function
  
# Take a list of numbers. 
my_list = [12, 65, 54, 39, 102, 339, 221, 50, 70, ]
  
# use anonymous function to filter and comparing 
# if divisible or not
result = list(filter(lambda x: (x % 13 == 0), my_list)) 
  
# printing the result
print(result) 

输出:

[65, 39, 221]

给定一个字符串列表,找出所有回文。

# Python Program to find palindromes in 
# a list of strings.
  
my_list = ["geeks", "geeg", "keek", "practice", "aa"]
  
# use anonymous function to filter palindromes.
# Please refer below article for details of reversed
# https://www.geeksforgeeks.org/reverse-string-python-5-different-ways/
result = list(filter(lambda x: (x == "".join(reversed(x))), my_list)) 
  
# printing the result
print(result) 

输出 :

['geeg', 'keek', 'aa']

给定一个字符串列表和一个字符串str,打印 str 的所有字谜

# Python Program to find all anagrams of str in 
# a list of strings.
from collections import Counter
  
my_list = ["geeks", "geeg", "keegs", "practice", "aa"]
str = "eegsk"
  
# use anonymous function to filter anagrams of x.
# Please refer below article for details of reversed
# https://www.geeksforgeeks.org/anagram-checking-python-collections-counter/
result = list(filter(lambda x: (Counter(str) == Counter(x)), my_list)) 
  
# printing the result
print(result) 

输出 :

['geeks', 'keegs']