List derived in Python

xiaoxiao2021-03-06  54

List completion (List comprehension) is an annoying, I can't remember the correct spelling of Comprehension: (To prevent me from this excellent characteristic, I still describe it.

The official tutorial says that List deduce is a simple method of establishing a list, usually an expression follows a FOR statement, then zero or more for or if statements. The LIST it returns is a result of performing an expression under the FOR and IF statement. If the result is a tuple, you must use parentheses "()" to enclose.

Oh, this explanation is also not easy to understand the English name. Look at the example in the official document:

>>> Freshfruit = ['Banana', 'loganberry', 'passion strip () for weapon in freshfruit] [' banana ',' loganberry ',' passion struit '] huh, no Excessive explanation, only one of the more easily acceptable ways to describe []:

For weapon in freshfruit: Weapon.Strip () More complex:

>>> VEC = [2, 4, 6] >>> [3 * x for x in Vec IF x> 3] [12, 18] and the corresponding operation is:

Vec = [2, 4, 6] for x in vec: if x> 3: 3 * x huh, this is better to understand, in summary, the derived of List is to start from the first FOR, will [ The first statement in the FOR is the last statement of the for, and its results are elements in the list.

转载请注明原文地址:https://www.9cbs.com/read-116436.html

New Post(0)