Python advanced (1)

1、* args and **kwargs

* args
  • args is used to send a variable number of parameter lists that are not key-value pairs to a function.

python

def test_var_args(f_arg,*argv):print("first normal arg:", f_arg)for arg in argv:print("another arg through *argv:", arg)test_var_args('yasoob','python','eggs','test')

This will produce the following output:
first normal arg: yasoob
another arg through *argv: python
another arg through *argv: eggs
another arg through *argv: test
**kwargs

****kwargs allows you to pass key-value pairs of indefinite length as parameters to a function. If you want to handle named parameters in a function, you should use ** **kwargs. For example, the parameter is a dictionary

Code

def greet_me(**kwargs):for key,value in kwargs.items():print("{0} == {1}".format(key,value))greet_me(name='michong',age=10)        

Output
name == michong
age ==10

The order of standard parameters and *args, **kwargs when used

Code

demo_func(fargs,*args,**kwargs)

2、 Debugging

pdb.set_trace()method#####

This method can also be used in jupter Notebook. When this method is used, put it directly at the place where you need to break

python

import pdb
def make_bread():
 pdb.set_trace()return"I don't have time"print(make_bread())
Commands in debugger mode#####

3、 Generator Generator####

Iterable

The object defines the iter method that can return an iterator, or defines the getitem method that can support subscript index, it is an iterable object

Iterator

If next or next is arbitrarily defined, it is an iterator

Iteration

The process of loop traversal is called iteration

Generators

It is also an iterator, using yield to generate a value

Code

def generator_function():for i inrange(10):yield i
for item ingenerator_function():print(item)
    
Output:
 # Output:0
    # 1
    # 2
    # 3
    # 4
    # 5
    # 6
    # 7
    # 8
    # 9

4、 Map, Filter and Reduce

Map
Traverse elements

Code

items =[1,2,3,4,5]
squared =list(map(lambda x: x**2, items))
Traverse method

Code

def multiply(x):return(x*x)
def add(x):return(x+x)
funcs =[multiply, add]for i inrange(5):
 value =map(lambda x:x(i), funcs)print(list(value))
# Translator's Note: In the last print, the list conversion was added, for python2/3 compatibility
# In python2 map directly returns a list, but in python3 it returns an iterator
# So in order to be compatible with python3,Need list conversion

# Output:
# [0,0]
# [1,2]
# [4,4]
# [9,6]
# [16,8]
Filter

filter filters the elements in the list and returns a column composed of all the elements that meet the requirements
Table, meets the requirements, that is, when the function is mapped to the element, the return value is True.

Code

number_list =range(-5,5)list(filter(lambda x:x%2==0, number_list))

output
    [-4,-2,0,2,4]
Reduce

To calculate a list and return the result, you can use the Reduce function

Code

# The following is the function of adding all the elements in the list to each other
from functools import reduce
reduce((lambda x, y:x+y),[1,2,3,4])

output:
    10

5、 set data structure####

set#####

Code

some_list =['a','b','c','b','d','m','n','n']
duplicates =set([x for x in some_list if some_list.count(x)>1])print(duplicates)

## Output:set(['b','n'])
Intersection

Code

valid =set(['yellow','red','blue','green','black'])
input_set =set(['red','brown'])print(input_set.intersection(valid))

output:{'red'}
Difference set

Code

valid =set(['yellow','red','blue','green','black'])
input_set =set(['red','brown'])print(input_set.difference(valid))

output:
{' brown'}
Basic operation
Add element

Code

s.add( x )

s.update( x ) ##You can also add elements, and the parameter can be a list
Remove element

Code

s.remove( x ) ##Remove the element x from the set s. If the element does not exist, an error will occur.
s.discard( x ) ##There is also a method to remove elements in the collection, and if the element does not exist, no error occurs.

s.discard( x ) ##There is also a method to remove elements in the collection, and if the element does not exist, no error occurs.

s.pop()  ##Randomly delete an element in the set
Count the number of collection elements######

Code

len(s)
Empty collection

Code

s.clear()
Complete list of built-in methods

Recommended Posts

Python advanced (1)
Python advanced knowledge points
Python advanced usage summary
Python multithreading
Python CookBook
Python FAQ
Python3 dictionary
Python3 module
Python basics
Python descriptor
Python basics 2
Python exec
Python3 tuple
Python decorator
Python IO
Python multithreading
Python toolchain
Python3 list
Python multitasking-coroutine
Python overview
python introduction
Python analytic
Python basics
07. Python3 functions
Python basics 3
Python multitasking-threads
Python functions
python sys.stdout
python operator
Python entry-3
Centos 7.5 python3.6
Python string
python queue Queue
Python basics 4
Python basics 5
Centos6 install Python2.7.13
Python basic syntax (1)
Python exit loop
Ubuntu16 upgrade Python3
Centos7 install Python 3.6.
ubuntu18.04 install python2
Relearn ubuntu --python3
Python2.7 [Installation Tutorial]
Python 3.9 is here!
Python study notes (1)
python learning route
CentOS7 upgrade python3
Python3 basic syntax
Python review one
linux+ubuntu solve python
Functions in python
Python learning-variable types
CentOS install Python 3.6
7 features of Python3.9
Python file operation
ubuntu12.04 install python3
Python design patterns
Python - centos6 installation
Centos7 install Python2.7
01. Introduction to Python
100 small Python examples