编程语言
首页 > 编程语言> > Python — Map, Filter, Reduce

Python — Map, Filter, Reduce

作者:互联网

地图

首先,让我们了解为什么我们需要一个map函数,以及它如何帮助我们减少工作并使代码更有效率。

假设我们有一个列表,如下所示,但列表的问题是,其中存在的所有元素都是字符串格式,因此如果我们尝试执行任何数值运算,它不会产生任何结果。

l1 = [“3”,“5”,“9”,“12”,“25”]x = l1[0]
+1
打印(x)
----------------------------------------------------------------------
-----
类型错误回溯(最近一次调用
)
输入 在 [1] 中,在<单元格行中:2>()1 l1 = [“3”,“5”,“9”,“12”,“25”]----> 2 x = l1[0]
+1
3 print(x)

类型错误:只能将 str(不是 “int”)连接到str

正如我们在上面看到的,代码中抛出了一个错误,指出我们尝试对其执行加法操作的对象是 str 格式,因此不能相同。这里的“x”是我们存储了带有 int 的 str 值相加元素的变量,需要更改。

现在解决这个问题的一种方法是 for 循环,让我们来看看

对于范围(len(L1)):
l1[i] = int(L1[i])

现在,如果我们执行与之前相同的操作,它不会抛出任何错误。一起来看看吧。

x = L1[0]+1
打印(x)
4

作为 l1 列表 3 的第一个元素,所以 1 被添加到它,结果我们收到了 4。但这不是一个理想的解决方案,因为每次编写 for loop 听起来都不可行,为了避免此类问题,我们借助 map 函数,其中一行代码就可以解决我们的问题。一起来看看吧

我们将采用另一个名为 l2 的列表,并将再次执行相同的操作。

l2 = [“3”,“5”,“9”,“12”,“25”]l2 = list(map(int,l2))y = l2[0]
+1
print(y)
4

如我们所见,在 map 函数的帮助下,它运行良好,请始终记住我们要在数据类型中操作的函数类型应始终是 map 函数内的第一个参数,在这里我们尝试将 str 元素转换为 int,因此在 map 关键字之后我们使用 int 参数作为第一个。

Now let’s have a look at a few other examples as well, suppose we want to find out the square values of an entire list, let’s see how we can do that with the help of the map function.

First, let’s write a function ‘sq’ which will return the squared value of a given number

def sq(a):
  return a*a

Now let’s create a list named l3 with integer elements

l3 = [3,5,7,2,9]
l4 = list(map(sq,l3))
print(l4)
[9, 25, 49, 4, 81]

As we can see, we are able to get the squared value for all the elements present in the list.

Now let’s look into something more complicated, what we are trying to achieve now is to get the square and cube of numbers from 0,8, stored in a list. We will use the map function, lambda function, and also our def keyword to describe the square and cube functions now. Then we will use a for loop to create the list.

def sq(a):
  return a*a
def cube(a):
  return a*a*a
func = [sq,cube]
for i in range(8):
  rslt = list(map(lambda x:x(i) ,func))
print(rslt)
[0, 0]
[1, 1]
[4, 8]
[9, 27]
[16, 64]
[25, 125]
[36, 216]
[49, 343]

Filter

As the name suggests, the filter function helps us in filtering a given set of iterables , as an argument it takes the stated function based on which we will be filtering our iterables and then it takes the iterables themselves as the arguments. Below 3 types of filters are given, one will filter numbers greater than 10 from the list, one will filter numbers lesser than 10, and one will have multiple conditioning that states the number should be greater than 10 but lesser than 15.

l1 = [34,12,5,8,11,14,19,10,1,50]
def grt_condi(num):
  return num>10
def lsr_condi(num):
  return num<10
def multi_condi(num):
  return num>10 and num<15

l2 = list(filter(grt_condi,l1))
l3 = list(filter(lsr_condi,l1))
l4 = list(filter(multi_condi,l1))

print(l2)
print(l3)
print(l4)
[34, 12, 11, 14, 19, 50]
[5, 8, 1]
[12, 11, 14]

Reduce

Let’s understand what reduce function does and what problem it solves, to understand this, let’s first take a problem statement and our traditional walkaround in solving that.

Let’s say there is a list and we want to get the summation of all the integers present in the list

l6 = [2,3,6,5,8,9,10]
x = 0
for i in l6:
  x = x+i
print(x)
43

但是同样,编写所有这些代码行似乎不像python,让我们减少代码行,看看我们在这里可以实现什么。还有一件事是,我们需要首先从 functools 导入 reduce 函数才能继续。

从 functools 导入 reduce
l7 = reduce(lambda x,y:x+y , l6)print(l7)
43

标签:Python,地图,软件开发
来源: