Advanced Functions
A ternary expression
Return Values When the condition else if condition is not satisfied when the condition is satisfied.
x = 1
if x == 1:
print(1)
else:
print(0)
print(1) if x == 1 else print(0)
1
1
A triplet of expressions only supports dual branch structure.
Second, the list of push formula
lt = []
for i in range(10):
lt.append(i**2)
print(lt)
lt = [i ** 2 for i in range(10)]
print(lt)
List pushed to which they are directly implemented exponentiation.
dic = {'a': 1, 'b': 2}
lt = [(k, v) for (k, v) in dic.items()]
print(lt)
[('a', 1), ('b', 2)]
List may be pushed to the formula or conversion dictionary into a list of tuples.
Third, the Dictionary of formula
Dictionary generated 3.1
dic = {'a': 1, 'b': 2}
new_dic = {k * 2: v ** 2 for k, v in dic.items()}
print(new_dic)
{'aa': 1, 'bb': 4}
Dictionary formula generation dictionary may also be arithmetic operations on k, v.
3.2 zip () method
zip (zipper function) is a compression method, a method of built-in python, () should be entered in iterative objects.
z = zip(['a', 'b', 'c', 'd'], [1, 2, 3, 4])
dic = {k: v ** 2 for k, v in zip(['a', 'b', 'c', 'd'], [1, 2, 3, 4])}
print(dic)
{'a': 1, 'b': 4, 'c': 9, 'd': 16}
zip method may be a list of tuples in the package into the dictionary.
Fourth, the generator
Builder: Custom iterator
4.1.1 yield keyword
def func():
yield 456
print(789)
yield 101112
print(131415)
f = func()
print(f)
f_iter = f.__iter__()
print(f_iter.__next__())
print(f_iter.__next__())
print(f_iter.__next__())
yield of three characteristics:
- yield function can become generator (custom iterator objects, and having __iter__ __next__ methods.
- yield function can be stopped, and then the next next run again yield the following code
- Yield of n generators have n elements, n can next time, the next time n + 1 will be given.
4.1.2 yield和return
return characteristics:
1. Return value
2. termination function
4.2 Collaborative Program
Coroutine (coroutine) generally refers to a function:
- There are different from each other local variables, the instruction pointer, but still shared global variables;
- You can easily suspend, resume, and a plurality of entry and exit points;
- Synergy between multiple programs running performance of collaboration, such as the result of a process run A to B in the need to continue to perform.
4.2.1 send(value)
The method of addition is next send another recovery generator.
4.2.2 close()
This method is used to close the generator. Call or send again to close next generation will raise StopIteration right.
4.2.3 throw(type,value=None,traceback=None)
Interrupt Generator is a very flexible technique can throw an exception through a GeneratorExit throw to terminate the Generator. Close () method effect is the same.
4.3 summary
yield:
To provide a custom iterator way
yield can live pause function, and provide a current return value
yield和return:
The same point: both are functions used internally, can return values, and return value does not limit the type and number of
Different points: return can only return once it; yield may return multiple values
4.4 generation expression
The list of derived formula [] with () is to generate expressions, advantages: save memory, a value is produced only once in memory.
Fifth, the anonymous function
Anonymous function is to function without a name, we need to use lambda, lambda + using the parameters: "block" format.
Anonymous functions generally not used alone, and the filter () / map () / sorted () / sort the list () method of the built-in combination.
If we want to retrieve from said dictionary max values, we can use max () method, but the max () is the default dictionary comparison key. First, it is necessary to become iterables iterator object, and res = next (iterator object), the parameters passed to the function key res as specified, then the return value of the function as a judgment basis.
If we want the above dictionary k, v according to descending order, you can use the sorted () method. First iterables into iterator object, and res = next (iterator object), res as the argument to the function specified by the first parameter, and returns the value as determined according to the function.
If we want a list of some k do processing, can use the map () method. One result iterables first iterator object then becomes res = next (iterator object), res as the argument to a function specified by the first parameter, and returns the value as the map () function of the method .
If we want to filter k in addition to containing a keyword, we can use the filter () method. First iterables into iterator object, and res = next (iterator object), res as the argument to a function specified by the first parameter, then the filter will determine the return value is true function, if true then leave.