Python special syntax
Python has some built-in special functions, which are very python-specific. Can make the code more concise.
You can see the example:
1 filter(function, sequence):
str = ['a', 'b', 'c', 'd']
def fun1(s): return s if s ! = 'a' else None
ret = filter(fun1, str)
print ret
## ['b', 'c', 'd']
Execute function(item) on the items in sequence one by one , the items whose execution result is True are composed into a List/String/Tuple (depending on the type of sequence) and returned.
can be regarded as a filter function.
2 map(function, sequence)
str = ['a', 'b', 'c', 'd']
def fun2(s): return s + ".txt"
ret = map (fun2, str)
print ret
## ['a.txt', 'b.txt', 'c.txt', 'd.txt']
Execute function(item) on the items in sequence one by one ), see the execution results form a List and return:
map also supports multiple sequences, which requires the function to also support a corresponding number of parameter inputs:
def add(x, y): return x+y
print map( add, range(10), range(10))
##[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
3 reduce(function, sequence, starting_value): def add1(x,y): return x + y
print reduce(add1, range(1, 100))
print reduce(add1, range(1, 100), 20)
## 4950 (Note: 1 +2+...+99)
## 4970 (Note: 1+2+...+99+20)
Iteratively calls the function on the items in the sequence. If there is a starting_value, it can also be used as the initial value. Call, for example, can be used to sum a List:
4 lambda:
g = lambda s: s + ".fsh"
print g("haha")
print (lambda x: x * 2) ( 3)
## haha.fsh
## 6
This is an interesting syntax supported by Python, which allows you to quickly define a single-line minimal function, similar to macros in C language, these functions are called lambda.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

Using python in Linux terminal...

Fastapi ...

Understanding the anti-crawling strategy of Investing.com Many people often try to crawl news data from Investing.com (https://cn.investing.com/news/latest-news)...
