Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How python uses Map and Filter functions

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces python how to use Map and Filter functions, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.

Map and Filter

Once you have mastered lambda expressions, you can achieve more powerful functionality by learning to use them with Map and Filter functions.

Specifically, map performs some action on each element in the list and converts it into a new list.

In this case, it iterates through each element and multiplies it by 2 to form a new list. (attention! The list () function simply converts the output to a list type)

# Map seq = [1,2,3,4,5] result = list (map (lambda var: var*2, seq)) print (result) [2,4,6,8,10]

The Filter function accepts a list and a rule, just like map, but returns a subset of the original list by comparing each element with a Boolean filter rule.

# Filter seq = [1, 2, 3, 4, 5] result = list (filter (lambda x: X > 2, seq) print (result) [3,4,5] Thank you for reading this article carefully. I hope the article "how python uses Map and Filter functions" shared by the editor will be helpful to you. At the same time, I hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report