python advanced features

Slicing: used to take some elements of a list or tuple;
L = ['Michael','Sarah','Tracy','Bob','Jack']
L[0:3] //Indicates taking from index 0 until index 3, but excluding index 3, that is, index 0, 1, 2; exactly three elements;
L[:3] //If the first index is 0, it can be omitted;
L[1:3] //Start at index 1 and end at 3-1;
L[-2:] //Start from the penultimate element, and finish the element;
L[-2:-1] //Start from the second-to-last element and get the first-to-last element;
Ok, now let's strengthen this processing method, we also want more functions, for example, take one every two;
L = list(range(100))
L[:10:2] //Start from index 0, (it can also be seen from here that range(100) refers to from 0 to 100-1), take 10-1, and then take 1 every two , which is 0,2,4,6,8
The string can also be seen as a kind of list, each element is a character. You can also use slice operations,
'ABCDEFG'[0,3]    //'ABC'
'ABCDEFG'[::2]    //'ACEG'

Iterate:
Dictionaries can also be implemented iteratively. However, dict iterates over keys. If you want to iterate over values, you can use for value in d.values(); if you want to iterate over keys and values ​​at the same time, use for k, v in d. items();
Iterate over the values ​​of the dictionary:
for n in d.values():
    print(n)
Iterate over the keys and values ​​of a dictionary:
for k,v in d.items():
    print (k, v)
 
In a for loop, it is normal to reference two variables at the same time:
for x,y in [(1.1),(2,4),(3,6)]:
    print(x,y)
When outputting, just use print(x,y) directly~~

List Compilation:
When writing a list comprehension, put the element x*x to be generated in the front, followed by a for loop, and the list can be created;
[x * x for x in range(100)] //Put the elements in range(100) after x, and generate a list according to x*x;
[m + n for m in 'ABC' for n in 'XYZ'] //Use two layers of loops to generate full permutations;

Generator: Creates a list, subject to memory constraints, and the list capacity is limited. We hope to continuously calculate the subsequent elements in the process of looping, so that there is no need to create a complete list, thus saving a lot of space, while looping and calculating, called generator: generator;
The first method is to change the [] of a list comprehension to (); a generator is created;
L = [x * x for x in range(100)]
g = (x * x for x in range(100))
print(next(g))            // 0
print(next(g))            //1

We already know that the data types that can be directly applied to the for loop are as follows:
One is a collection data type, such as list, tuple, dict, set, str, etc.;
One is generator, including generator and generator function with yield;
Objects that can act directly on for loops are collectively called iterable objects: iterable;
An object that can be called by the next() function and continuously returns the next value is called an iterator: iterator;
Use isinstance() to determine whether an object is an iterator object;
Generators are all iterator objects, but list, dict, and str are iterables but not iterators;
To turn list, dict, str and other iterables into iterators, you can use the iter() function;
Note: All objects that can be applied to the for loop are of the iterable type;
Any object that can act on the next() function is an iteratorlexington, which represents a sequence of lazy computations;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325061324&siteId=291194637