python advanced features

Hello everyone, this is Longbow

In the first few articles, we learned the basic syntax and functions of python

When developing applications, we all expect a high development efficiency and a small amount of code

Therefore, today we will learn the advanced features of python

                                          

content

slice

list and tuple slices

str slice

iterate

list comprehension

Generators and Iterators

Builder

 iterator


slice

list and tuple slices

We have learned about list and tuple before, and we often need to extract the elements. Let's practice

Let's look at the first two lines first, the most basic, we build a new list, take elements from L and put them in

 But this method cannot be extended. For example, we want to take out 3 data, or 4 data?

We can use the following loop method, only need to change the value of n, we can correspond to the elements taken out

But writing this string of code every time is not necessarily too trivial, so people have come up with a better way to slice

                                            

 

 This is the slice operation of L = ['Zhang San', 'Li Si', 'Wang Wu', 'Zhao Liu'], we know that list can take out the corresponding element from the subscript, L[0:3] is to take out , the elements whose subscripts are 0, 1, and 2, that is, do not include 3. When the second line is 0, it can be omitted.

list[-1] is the last element in the list, which can also be used here, L[-2:] is the second-to-last element to the last element, and L[-2:-1] is before the last element , so it does not include 'Zhao Liu'.

str slice

Finally, not only list and tuple can be sliced, but also str string type

The first is to read the first 4 elements, and the second is to read every 3 elements.

                                               

 Finally, we can think about why it is called slicing?

For example, this 'ABCDEFG'[:4], we take a part of the whole source data, but it's not like cutting a piece from a ham (there is too little data here, it should be called segmenting)

iterate

The second part is iteration, so what is iteration?

The process of accessing elements in a list or tuple through a for...in statement is iteration

add = [1,2,3,4,5]
for x in add:
    print(x)

This process is iteration, but it is not limited to the list structure

What data can be iterated over? How to judge ?

The type collections.abcof the module we need to callIterable

Here it is explained that both strings and lists, tuples can be iterated

Pay attention to capitalization here, otherwise an error will be reported as above

                                  

 Before we used for to iterate all one element, it can also have multiple elements at a time

list comprehension

We sometimes construct some special series, such as [1,3,5,7,9]

Let's use the knowledge we have learned before to realize

But what if we want to construct the square of [1,3,5,7,9]?

We can extend the second way to do

  

 But to simplify this process, there is a list comprehension

One line of code 

                                           

 Compared with for...in, the above list comprehension has only one more x*x, we can also add if judgment

Modify the previous x*x, we can generate the previous [1,3,5,7,9]

Generators and Iterators

Builder

We learned above how to use list comprehension to generate lists

But when we have a lot of data, it is extremely unfriendly to memory if the entire list is generated at once when calling

We only expect to call out a part of the data we need, that's why the generator

A generator is a generator, so we use G to represent it, just change the [] of the list generator to ()

 We access the element by next (generator name)

Also be careful not to access out of bounds here, otherwise an error will be reported

                                            

It is also possible to iterate over the generator using the iteration we talked about above

 

 We talked about the common usage of generators above, so I will leave you a question today

How are generators used in functions? Welcome to comment in the comment area

                                         

 iterator

We talked about iteration above, and what can be iterated by for is called an iterable object: Iterable

We can use the isinstance() interface to determine whether a data type is an iterable object

list, tuple, dict, set, str,generator are all iterable objects

An object that can benext() called by a function and constantly returns the next value is called an iterator :Iterator

Then we can get the relationship between the two

For example list, dict,str虽是可迭代对象,却不是迭代器

那他们可以使用next()吗?

 

答案是还有机会,我们使用iter()可将其变成迭代器

 

 

Today's content is over

Looking forward to positive reviews

See you next time everyone ...

Guess you like

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