科学使用python中的Map函数和Filter函数

欢迎关注 “小白玩转Python”,发现更多 “有趣”

map函数

假设我们想要用现有列表创建一个新的列表,也就是说要在现有列表的基础上,对列表的每个元素进行某种操作,然后将结果输出来创建一个新的列表。例如,有一个数字列表,而我们想创建一个包含其平方的新列表。 实现此目的的一种方法是使用for循环遍历数字列表,并返回每个数字或每个元素的平方。 并且,在遍历列表时,将平方值添加到新列表中。 

让我们来看看如何用代码来实现这一点:

# defining a function that returns the square of a number
def squared(num):
  return num**2


# original list
num_list = [1,2,3,4,5,6]


# list that will contain the squared numbers
num_list_squared = []


# using a for loop to iterate over our num_list and create a new list with the squared values
for num in num_list:
  num_list_squared.append(squared(num))
  
print(num_list_squared) # output is [1,4,9,16,25,36]

我们有一个数字列表num_list,我们要创建一个新列表num_list_squared,它的每个元素是对应num_list每个元素的平方。我们使用for循环遍历num_list,并将num_list每个元素的平方附加到num_list_squared列表中。

完成此操作的另一种方法是使用内置的 python 函数 map。Map 函数接受两个参数: 我们要应用的函数和我们要应用它的迭代对象或序列(例如本例中的列表)。换句话说,map 函数映射或应用这个函数到我们传入的序列的每个元素。

map 函数使用的格式如下:

map (要应用的函数,要应用的元素序列)

这个 map 函数将返回一个 map 对象,它是一个迭代对象。如果我们想从这个 map 对象创建一个列表,我们需要将 map 对象传递给内置的 list 函数,如下所示:

list(map(函数, 序列))

让我们看看如何使用内置的 map 函数来完成上面的代码:

# defining a function that returns the square of a number
def squared(num):
  return num**2


# original list
num_list = [1,2,3,4,5,6]


# using the map function to create this new list of squared values
num_list_squared = list(map(squared, num_list))
  
print(num_list_squared) # output is [1,4,9,16,25,36]

请记住,我们可以将 map 函数应用于任何迭代对象或序列中的每个元素,而不仅仅是一个列表。

让我们来分析一下这行代码中发生了什么:

num_list_squared = list(map(squared, num_list))

Map 函数从 num_list 中获取第一个元素,即1,并将其作为参数传递给 squared 函数(因为我们将该函数作为 map 函数的第一个参数传递给了该函数)。然后squared 函数返回1的平方,也就是1,然后它被添加到我们的 map 对象中。Map 函数接着从 num_list 中获取第二个元素,即2,并将其作为参数传递给 squared 函数。函数的平方返回2的平方,也就是4,然后这个平方被添加到我们的 map 对象中。当它完成 num_list 元素的处理,其余的平方数字被添加到 map 对象后,list 函数将这个 map 对象强制转换到一个 list 中,然后这个 list 被赋值给变量 num_list_squared。

使用 Lambda 表达式:

我们可以通过传入一个 lambda 表达式作为函数来进一步缩短代码:

# original list
num_list = [1,2,3,4,5,6]


# using the map function to create this new list of squared values
num_list_squared = list(map(lambda x:x**2, num_list))
  
print(num_list_squared) # output is [1,4,9,16,25,36]

注意: 我们传入映射的函数也可以是 python 中的内置函数。例如,如果我们有一个字符串列表,我们想要创建一个包含这些字符串长度的新列表,我们可以像下面这样传入内置的 len 函数:

list(map(len, list_of_strings))

Filter函数

同样,假设我们想使用已有的列表创建一个列表。但是这次我们希望新的列表只包含满足给定条件的元素。例如,我们有一个数字列表,我们想要创建一个只包含列表中偶数的新列表。我们可以使用 for 循环来完成这个任务,如下所示:

# create function that returns True if the number passed in is even and False if it is not even
def is_even(num):
  return num % 2 == 0


# original list of numbers 
list_of_nums = [1,2,3,4,5,6]


# new list that will contain only the even numbers from list_of_nums
list_of_even_nums = []


# iterate over the list_of_nums. If the num is even, it adds it to list_of_even_nums
for num in list_of_nums:
  if is_even(num) == True:
    list_of_even_nums.append(num)


print(list_of_even_nums) # output would be [2,4,6]

我们有一个数字列表list_of_nums,其中包含数字1、2、3、4、5和6。我们要创建一个新的数字列表list_of_even_nums,该列表仅包含list_of_nums中的偶数。因此,我们创建了一个函数is_even,该函数接受一个输入,如果该输入为偶数,则返回True,否则为False。然后,我们创建了一个for循环,该循环遍历list_of_nums并通过将该元素传递给is_even函数来检查该列表的每个数字是否均是偶数。如果is_even函数返回True,则该数字将附加到list_of_even_nums。如果is_even返回False,则该数字不会附加到list_of_even_nums中。

实现这一点的另一种方法是使用内置的 python 函数 filter。Filter 函数接受两个参数: 检查特定条件的函数和我们希望将其应用到的序列(例如本例中的列表)。Filter 函数从我们的列表中获取每个元素,并将其传递给我们提供的函数。如果使用特定元素作为参数的函数返回 True,那么 filter 函数将向 filter 对象添加该值(然后我们可以像对待上面的 map 对象一样创建一个列表)。如果函数返回 False,那么该元素将不会被添加到我们的过滤器对象中。换句话说,我们可以把Filter函数看作是基于某种条件过滤我们的列表或序列。

Filter函数格式如下:

Filter(检查条件的函数,我们要应用它的元素序列)

这个过滤器函数将返回一个过滤器对象,这是一个迭代对象。如果我们想从这个 filter 对象创建一个 list,我们需要将 filter 对象传递给内置的 list 函数(就像我们对 map 对象所做的一样) ,如下所示:

list(filter(函数, 序列))

现在让我们使用内置的 filter 函数创建与上面相同的列表:

# create function that returns True if the number passed in is even and False if it is not even
def is_even(num):
  return num % 2 == 0


# original list of numbers 
list_of_nums = [1,2,3,4,5,6]


# new list that will contain only the even numbers from list_of_nums
list_of_even_nums = list(filter(is_even, list_of_nums))


print(list_of_even_nums) # output would be [2,4,6]

Filter 函数从 list_of_nums 中获取第一个元素,这是一个1,并将其作为参数传递给 is_even 函数(因为我们将该函数作为filter函数的第一个参数传递给了filter函数)。函数 is_even然后返回False,因为1不是偶数,所以1没有添加到我们的filter对象中。Filter函数接着从 list _of_nums 中获取第二个元素,即2,并将其作为参数传递给 is_even 函数。函数返回 True,因为2是偶数,所以2被添加到我们的filter对象中。在遍历 list_of_nums中的其余元素并将其余偶数添加到我们的filter对象之后,list 函数将这个filter对象强制转换到一个list中,并将该list分配给变量 list_even_nums。

使用 Lambda 表达式:

我们可以通过传入一个 lambda 表达式作为函数来进一步缩短代码:

# original list of numbers 
list_of_nums = [1,2,3,4,5,6]


# new list that will contain only the even numbers from list_of_nums
list_of_even_nums = list(filter(lambda x: x%2==0, list_of_nums))


print(list_of_even_nums) # output would be [2,4,6]

总结:

在本文中,我们学习了 python 内置的map和filter函数是如何工作的。我们还看到了一些使用它们的例子。最后,我们看到了如何将 lambda 表达式作为参数传递给这些函数。

·  END  ·

HAPPY LIFE

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值