Home Backend Development Python Tutorial python基础教程之lambda表达式使用方法

python基础教程之lambda表达式使用方法

Jun 16, 2016 am 08:45 AM
lambda expression

Python中,如果函数体是一个单独的return expression语句,开发者可以选择使用特殊的lambda表达式形式替换该函数:

复制代码 代码如下:

lambda parameters: expression

lambda表达式相当于函数体为单个return语句的普通函数的匿名函数。请注意,lambda语法并没有使用return关键字。开发者可以在任何可以使用函数引用的位置使用lambda表达式。在开发者想要使用一个简单函数作为参数或者返回值时,使用lambda表达式是很方便的。下面是使用lambda表达式作为内置filter函数的一个参数的示例:

复制代码 代码如下:

aList = [1, 2, 3, 4, 5, 6, 7, 8, 9]
low = 3
high = 7
filter(lambda x, l=low, h=high: h>x>l, aList) # returns: [4, 5, 6]

作为另外一种选择,开发者还可以使用一个可以为函数变量命名的本地def语句。然后,开发者可以使用这个名称作为参数或返回值。下面是使用本地def语句的相同filter示例:

复制代码 代码如下:

aList = [1, 2, 3, 4, 5, 6, 7, 8, 9]
low = 3
high = 7
def within_bounds(value, l=low, h=high):
return h>value>l filter(within_bounds, aList) #
returns: [4, 5, 6]

因为lambda表达式只是偶尔有用,许多Python用户更喜欢使用def, def要更通用些,如果开发者为函数选择了一个比较合理的名称,会让代码具有更好的可读性。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Master functional programming and Lambda expressions in Go language Master functional programming and Lambda expressions in Go language Nov 30, 2023 am 10:46 AM

In the contemporary programming world, Functional Programming (FP for short) has gradually become a popular programming paradigm. It emphasizes using functions as basic building blocks to build programs, and regards the calculation process as the continuous transfer and conversion between functions. In recent years, Go language (also known as Golang) has gradually been widely used in various fields due to its simplicity, efficiency, concurrency safety and other characteristics. Although the Go language itself is not a purely functional programming language, it provides sufficient functionality.

How to do functional programming with PHP How to do functional programming with PHP Jun 06, 2023 am 08:21 AM

PHP is a widely used server-side language. One of the reasons why many web developers like to use PHP is its rich function library and simple and easy-to-use function syntax. Functional programming is a programming paradigm that well encapsulates data and behavior, making the code more modular and easy to maintain and test. In this article, we will introduce how to use PHP for functional programming. Functional Programming Basics The core idea of ​​functional programming is to treat functions as first-class citizens. Functions themselves can be passed, returned, and composed like variables.

Concurrent programming methods using Lambda expressions Concurrent programming methods using Lambda expressions Aug 20, 2023 am 08:17 AM

An important addition to JavaSE8 is the lambda expression feature. Use expressions to express method interfaces clearly and concisely. Collection libraries are very helpful. Collections can be iterated, filtered, and data extracted for useful purposes. To implement functional interfaces, lambda expressions are widely used. It saves a lot of code. Lambda expressions allow us to provide implementations without redefining methods. It is only here that the implementation code is formed by writing the code. The compiler does not create a .class file because Javalambda expressions are treated as functions. Functional interface @FunctionalInterface is a Java annotation that declares an interface as a functional interface.

Tips on using Lambda expressions in Java Tips on using Lambda expressions in Java Jun 16, 2023 am 09:07 AM

In Java 8, Lambda expressions are a very powerful tool that can greatly simplify code, making it more compact and easier to read. Lambda expressions can also improve the reusability and maintainability of code, thus effectively promoting development efficiency. This article will introduce some tips for using Lambda expressions in Java to help developers make better use of this powerful tool. Tip 1: Use Lambda expressions instead of anonymous inner classes before Java8 if we need

How to use LINQ and Lambda expressions in C# to improve code efficiency and solutions How to use LINQ and Lambda expressions in C# to improve code efficiency and solutions Oct 08, 2023 pm 03:37 PM

How to use LINQ and Lambda expressions in C# to improve code efficiency and solutions Introduction: With the continuous progress and development of software development, code efficiency has gradually become one of the important aspects that developers pay attention to. As a powerful programming language, C# provides many tools and features to improve the efficiency of your code. Among them, LINQ and Lambda expressions are two very useful features that can help developers write efficient code quickly and concisely. This article will introduce how to use LINQ and Lambda tables in C#

How to use Lambda functions in Java for functional programming How to use Lambda functions in Java for functional programming Jun 26, 2023 pm 04:04 PM

Functional programming is a very popular programming paradigm that emphasizes the use and composition of functions instead of statements and instructions in traditional imperative programming. Lambda functions added in Java 8 make functional programming in Java easier. In this article, we will cover the basics of Lambda functions and how to use it for functional programming. Definition of Lambda function A Lambda function is an undeclared method that can receive parameters and return values ​​and can be passed like an object.

Why can't Python's lambda expressions contain statements? Why can't Python's lambda expressions contain statements? Aug 19, 2023 am 08:14 AM

Yes, Python Lambda expressions cannot contain statements. Before we delve into the reasons, let’s understand what Lambda is, its expressions and statements. Lambda expressions allow the definition of anonymous functions. A Lambda function is an anonymous function without a name. Let’s look at the syntax − lambdaarguments:expressions keyword lambda defines a lambda function. A lambda expression contains one or more parameters, but it can only have one expression. Lambda Example Let’s look at an example −myStr="Thisisit!"(lambdamyStr:pr

Local variable type inference in Java 10: How to use var keyword in lambda expression Local variable type inference in Java 10: How to use var keyword in lambda expression Aug 02, 2023 pm 04:25 PM

Local variable type inference in Java10: How to use the var keyword in lambda expressions Introduction: Java10 introduces a new feature of local variable type inference, which allows us to use the var keyword to infer the type of a local variable when declaring it. While this feature may not be necessary in most cases, in some cases it can improve code readability and simplicity. This article will focus on how to use the var keyword in lambda expressions to implement local variable type inference.

See all articles