Pythonic Programming example and brief analysis

Source: Internet
Author: User

1. List

list[start: End: Increment]

Value passing and address passing

A = [2,1= a      # address delivery  B = a[:] Value passed a.sort ()print a    # [up] print b    #[+]

2. Sorting sort ()

 d = {1: " s   ", 2:"  a   "    sorted (D.items (), key = lambda  x:x[1] ) /* Sort by the 2nd element of the list element */
# Sorted (iterable, cmp=none/* function */, key=none/* function */, Reverse=false)  = ['aaa','a','aa'  = len)#The key value is the total function to be called print ll
Print sorted ("Sorting1234", Key=lambda x: (X.isdigit (), X.isdigit () and int (x)% 2 = = 0, X.isupper (), X.islower (), x)) #[' G ', ' i ', ' n ', ' O ', ' r ', ' t ', ' S ', ' 1 ', ' 3 ', ' 2 ', ' 4 ']

3. anonymous function lambda

# Lambda parameter: return value Lambda x:x+1

4....for. ... in ... if ...

A = [All-in-a-z]printforif i>0]

5.filter map Reduce

Filter (function, sequence): Executes function (item) on item in sequence, and the item that executes the result of true is composed of a list/string/ Tuple (depending on the type of sequence) returned

Print Lambda x:x!='a'abcd ')   #' BCD ' 

Map (function, sequence): Executes function (item) on item in sequence, and the result of execution consists of a list return:

Print Lambda x:x+1, Range (3))  #[+]

Reduce (function, sequence, starting_value): Calls the function in the order of the item in the sequence, and if there is a starting_value, it can also be called as an initial value. For example, you can use the list to sum

Print Lambda x,y:x+y, Range (3), ten)  #

6. Generator Generator

 for  in range (3)) for in a:print I

7. Range(start , end , increment )

Print  Range (1,9,2)  #[1, 3, 5, 7]

8...? ...:...

if else b    # fully equivalent andor  [c]) [0] (andor C    # not exactly equivalent, B cannot be false

F

Pythonic Programming example and brief analysis

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.