python-Generator-Advanced ~ various derivation

Source: Internet
Author: User

List-derived

Example one: Any number within 30 that can be divisible by 3

List derivation 1
 for inch if  is 0] Print (multiples) # Output: [0, 3, 6, 9, (+), (+) ,-

Example two: The square of all numbers within 30 that can be divisible by 3

List Derivation 2

Example three: Find all names in a nested list with two ' E ' names

Nested list Expressions
names = [['Tom','Billy','Jefferson','Andrew','Wesley','Steven','Joe'],         ['Alice','Jill','Ana','Wendy','Jennifer','Sherry','Eva']]Print([Name forLstinchNames forNameinchLstifName.count ('e') >= 2])#Note the traversal order, which is the key to the implementation 

Dictionary derivation type

Example one: Swap the key and value of a dictionary

dictionary derivation type one
Mcase = {'a'b': in Mcase}print(mcase_frequency)

Example two: Merge the value values of the case, unify the K into lowercase

Dictionary derivation Formula Two
Mcase = {'a' b'a' Z ': 3 for in Mcase.keys ()}print(mcase_frequency)

Set Deduction formula

Example: Calculating the square of each value in a list, with self-weight function

Set Deduction Formula One

Exercises:

Example 1: Filter out a list of strings that are less than 3 long and convert the rest to uppercase

Example 2: (x, y) where x is an even number between 0-5 and Y is an odd-numbered list of Ganso between 0-5

Example 3: A list of 3,6,9 in M = [[1,2,3],[4,5,6],[7,8,9]]

Answer
 for inch if len (name) >3forif in if y%2==1   for in

python-Generator-Advanced ~ various derivation

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.