Objective
New Year is not completely idle, every day with a little bit of time to learn something, this article for you to introduce a few Python operation details, including all, anything, for in and other operations, as well as introduce me to solve the problem of thinking.
First, the outset
Start with a simple statement that I see.
='拍照'anyinforin ["拍照""拍张照"])
When I saw this statement, I completely lost my whole person, and I could not understand what it was doing. But in fact, programming difficult is not these things, these things no longer afraid, can also find ways to solve, and hidden behind the programming thinking and mathematics and other knowledge is difficult to solve.
1.1 Any, all
After seeing this statement, I thought about how to simplify the complex problem.
So I checked the any function, and the any and all functions are for the collection. The any function is used to determine whether a given iteration parameter, iterable, is all empty, and if it is null, 0, FALSE, returns false if not all NULL, and returns TRUE if all is not empty, and if all is not NULL, the Otherwise, false is returned.
1.2 For in
This means that the above statement any inside is a Iterable object. Let's take a look at the statement inside:
='拍照'inforin ["拍照""拍张照"])
It is obvious that a for,for in Python is obvious, simply looping a set and filtering and computing the set. Also simplify it first:
forin ["拍照""拍张照"])
It's obvious that the generated collection is the same as the original, but what we usually see is this:
forin ["拍照""拍张照"]]
This involves the concept of a Python generator.
1.3 Generators
The advantage of the generator is that it does not need to be calculated beforehand, it only needs to be called when needed, and does not waste storage space and computing time. Simply put, a generator is a special set, which is not calculated as a common set, but only when it is needed. In fact, the implementation of the way is very simple, will [] replace (), then there is the above statement. Other operations and how to generate the generator can be online access to relevant information, not to repeat this.
1.4 Continuation for in
Then the for-in continues, and the result is the same as the (word for word in ["拍照", "拍张照"])
original data, so if we want to make a little change, we're going to have to execute a function on the first word, and then there's a function here that says that word in text
word is in text, That is, whether word is a substring of text, equivalent to the contain of other languages.
This makes it very clear that the (word in text for word in ["拍照", "拍张照"])
loop primitive collection, which determines whether each element is a substring of the given text string, returns a true, False collection.
Of course, we can also implement filtering in the loop, see the following statement:
forin ["拍照""拍张照"ifin text)
It seems that the difference between this statement and the above is that the position of Word in text has changed, and there is a more if. In fact, the difference is very large, the function here is to determine whether word is a substring of text, if it is removed, the equivalent of filtering out the object is not a substring, and ultimately return a collection of strings.
1.5 Overall
So here we look at the first statement:
='拍照'anyinforin ["拍照""拍张照"])
By combining the above statements, it is possible to know that this statement implements a loop set that determines whether each string is a substring of the given text, and returns True if it is not completely empty, that is, text returns true whenever any one of the strings in the collection is contained. can be used for command detection and other occasions.
Second, summary
In this paper, we introduce some common functions and concepts in Python by a slightly complicated statement, and analyze the complex and simple process.
Python analyzes several common functions and concepts through a single statement