Python filter List __python

Source: Internet
Author: User
As you know, Python has the power to map lists to other lists through list resolution. This ability is used in conjunction with filtering mechanisms to allow some elements in the list to be mapped while skipping other elements.
Filter List Syntax:
[Mapping-expression for element in Source-list if filter-expression]

This is the extension of the list resolution that you know you love. The first three parts are the same; the last part, the filter expression at the beginning of the IF. A filter expression can be any expression that returns a true or False value (almost anything in Python). Any element in which the filter expression calculus value is true can be included in the map. Other elements are ignored, they do not go into the mapping expression, and are not included in the output list.


1. List Filter Introduction

>>> li = ["A", "Mpilgrim", "foo", "B", "C", "B", "D", "D"]
>>> [elem for Elem in Li if Len (elem) ; 1]       (1)
[' Mpilgrim ', ' foo ']
>>> [elem for Elem in Li if Elem!= "B"]         (2)
[' A ', ' Mpilgrim ', ' fo O ', ' C ', ' d ', ' d ']
>>> [elem for Elem in Li if Li.count (elem) = = 1] (3)
[' A ', ' Mpilgrim ', ' foo ', ' C ']

(1) The mapping expression here is simple (just return the value of each element), so focus on the filter expression. Because Python traverses the entire list, it executes a filter expression on each element. If the filter expression calculus value is true, the element is mapped and
The result of the mapping expression will be included in the returned list. Here, you filter out all single character strings, leaving a list of long strings.
(2) Here you filter out a specific value of B. Note that this filter will filter out all B, because every time you remove B, the filter expression will be false.
(3) Count is a list method that returns the number of times a value appears in the list. You can assume that this filter will remove duplicate elements from the list and return a list that contains only copies of unique values in the original list. But that's not the case, because the values that appear two times in the original list (in this

For example, B and D are completely eliminated. There are several ways to exclude duplicate values from a list, but filtering is not one of them.


Return to this line in apihelper.py:

MethodList = [method to Method in Dir (object) if callable (GetAttr (object, method))

The line looks complicated-and it's really complicated-but the basic structure is the same. The entire filter expression returns a list and assigns a value to the MethodList variable. The first half of an expression is a list-mapped part. A mapping expression is an expression that is the same as the traversal element, so it returns the value of each element. Dir (object) returns the list of properties and methods of object objects-the list you are mapping. So the only new part that appears is the filter expression that follows the IF.
The filter expression looks scary, but it's not. You already know callable, getattr and in. As you can see in the previous section, if object is a module and method is the name of a function in the module above, the expression GetAttr (object, method) returns a function object.
So this expression receives an object named objects and then gets the list of its properties, methods, functions, and other members, and then filters out the members we don't care about. The filtering behavior is performed by invoking the GetAttr function on the name of each property/method/function to obtain a reference to the actual member, and then checking whether the member objects are callable, and of course these callable member objects may be methods or functions, and may also be built-in (e.g. pop methods for lists). or user-defined (such as the Odbchelper module's buildconnectionstring function). Here you don't have to care about other attributes, such as the __name__ properties built into each module.
Related Article

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.