Learn Python's everyday 3

Source: Internet
Author: User
Tags iterable

Some of the advanced features of Python:

Slice (just like the name, take a whole thing and choose what you want to cut off): by slicing you can quickly remove some elements, as long as you determine the index position, avoid the loop caused by the multi-write code

arrays, tuples, and strings can be sliced to get elements, [x:y] to index from index position x to Y position, excluding Y

[x:] Start index to end from X, [: Y] from 0 to Y (not including y), [x:y:z]z indicates a number per Z number.

Iterations: Iterations in Python are done through for...in, and as long as there are iterative objects, you can iterate, Dict storage is not based on

The list is arranged in order, so it may be different in the order of the results, the default iteration key in Dict, with the for value in X.values () can be

To implement iterative value, use the for X, y in D.items () to iterate over both key and value in Dict, using the iterable type of the collections module to judge,

From collections Import iterableisinstance (' abc ', iterable)

If you are implementing a Java-like table loop for the list, the enumerate function in Python can turn the list into an index-element pair

For I,value in enumerate ([' A ', ' B ', ' C ']):    print (I,value)
...
0 A
1 B
2 C

In a For loop, you can reference two variables at a time.

for (x, y) in ([1,1],[2,3],[3,9]):    print (x, y)

List generation: List generation with very concise code to create a list, according to their own needs, you can do different operations on the same variable to generate a list, you can also operate on two variables to generate a list, You can also call a method to generate a list of requirements (if the data type in the original list is different, you can add an additional judging condition, if isinstance (x, data type).



[S.xxxx () for s in L]

Generators: Changing the list generation to generator, or the generator,generator of complex logic through functions, works by continually calculating the next element during the for loop,

and end the For loop in the appropriate condition. The generator of the function, the return statement or the last line of execution to the function body, is the end of the generator instruction, and the For loop ends with it.

Iterators: Objects that can be used for loops are iterable types, objects that can act on the next () function are iterator () types, represent a sequence of lazy computations, do not know the length of the sequence in advance, and can only calculate the next data on demand with the next () function. Calculations are performed only if the next data needs to be returned.

Functional Programming:

In higher-order functions, the map () function receives two parameters, a function, and a iterable,map is used for each element in the sequence.

and returns the result as a new iterator; the reduce () function also receives two parameters, and the difference is that the reduce uses a function

element, the result of the element processing continues to participate in the processing of the function as a new element, and finally returns a result.

The filter () function is a filter, using the same usage as map (), but the result is returned only if the result is true, and the rest is discarded, returning a iterator.

The sorted () function is a function for sorting as a high-order function that can receive a key function to implement a custom sort, sort the keys, and then display the corresponding value sort results.

A reverse sort can pass in the third parameter reverse = True.

return function: function can return a result of a calculation, or return a function, return a function, this function is not executed, so it is best not to refer to variables that may change

anonymous function: Lambda x:x*x where x is the parameter of the function, the result of the expression of the anonymous function is his return value.

Adorner: itself is a higher-order function that can return a function, and then the function can be enhanced, where functools.wraps can make the function's. __name__ Property not replaced

 

Learn Python's everyday 3

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.