- 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 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)
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())
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
If next or next is arbitrarily defined, it is an iterator
The process of loop traversal is called iteration
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
Code
items =[1,2,3,4,5]
squared =list(map(lambda x: x**2, items))
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 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]
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
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'])
Code
valid =set(['yellow','red','blue','green','black'])
input_set =set(['red','brown'])print(input_set.intersection(valid))
output:{'red'}
Code
valid =set(['yellow','red','blue','green','black'])
input_set =set(['red','brown'])print(input_set.difference(valid))
output:
{' brown'}
Code
s.add( x )
s.update( x ) ##You can also add elements, and the parameter can be a list
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
Code
len(s)
Code
s.clear()
Recommended Posts