Python---Filter

Source: Internet
Author: User

The # filter# python built-in filter () function is used for filtering sequences like #  and map (), and filter () also receives a function and a sequence. Unlike map (), filter () applies the incoming function to each element in turn, and then decides whether to persist or discard the element based on whether the return value is True or false #  for example, in a list, delete even numbers, keep only odd numbers, you can write def  Is_odd (N):     return n % 2 == 1l = list (Filter (is_odd, &NBSP;[1,&NBSP;2,&NBSP;4,&NBSP;5,&NBSP;6,&NBSP;9,&NBSP;10,&NBSP;15]) print (l) #  Delete the empty string of a sequence, so you can write def  not_empty (s):     return s and s.strip () l = list (Filter (not_ empty, [' A ',  ',  ' B ', none,  ' C ',  '    ')) print (L) #  with the filter this higher order function, The key is to implement a "filter" function correctly #  notice that the filter () function returns a iterator, which is an inert sequence, so to force filter () to complete the result of the calculation, you need to use the list () function to get all the results and return the list#   #  a method of calculating prime numbers by using the filter of prime numbers, and its algorithm is very simple to understand #  first, list all the natural numbers starting at 2, and construct a sequence # 2, 3, 4,  5,&NBSP;6,&NBSP;7,&NBSP;8,&NBSP;9,&NBSP;10,&NBSP;11,&NBSP;12,&NBSP;13,&NBSP;14,&NBSP;15,&NBSP;16,&NBSP;17,  18, 19, 20, ...#  takes the first number 2 of a sequence, whichMust be a prime number, then sift out the # 3, 5, 7, 9, 11, 13, 15, 17, 19, ...# in multiples of 2 of the 2 sequence   Take the first number of the sequence 3, it must be a prime, and then sift out the # 5, 7, 11, 13, 17, 19, ...#  in multiples of 3 of the 3 sequence. Take the first number of the new sequence 5, and then sift out the # 7, 11, 13, 17, 19, ...#  of the 5 in multiples of the 5 sequence, and you can get all the primes #  Using Python to implement this algorithm, you can first construct an odd sequence starting from 3 Def _odd_iter ():    n = 1     while True:        n = n + 2         yield n#  Note that this is a generator and is an infinite sequence #  then define a filter function Def _not _divisible (n):    return lambda x: x % n > 0#  Finally, define a generator that continually returns the next prime Def primes ():     yield 2    it =  _odd_iter ()     while true:        n  = next (IT)         yield n        it =  filter (_not_divisible (n),  it) #  This generator first returns the first prime number 2, and then uses filter () to continuously generate the filtered new sequence #  because of the primes () is also an infinite sequence, so calls need to set a condition for exiting the Loop for n in primes ():    if n <  1000:        print (n)     else:         break#  Note that iterator is a sequence of lazy computations, so we can use Python to represent "all natural numbers", "All primes", and the code is very concise.


Python---Filter

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.