Python Learning (v): Easy to forget knowledge points

Source: Internet
Author: User

1. List comparison function cmp

>>> a = [1,2,3,4]>>> b = [1,2,3,4,5]>>> c = [1,2,3,4]>>> cmp (A, B) -1>>> CMP (a , c) 0

2, list parsing, code simplification

>>> a[1, 2, 3, 4]>>> d = []>>> for i in a:   ...  D.append (i + 2) ... >>> d[3, 4, 5, 6]>>> e = []>>> E = [i + 2 for i in A]>>> e[3, 4, 5, 6]

3. Dictionary creation

>>> d = Dict.fromkeys ([' Today ', ' Tomorrow '],20) >>> d{' tomorrow ': +, ' Today ': 20}

4. Set particularity

>>> s = {1,2,3,4}>>> t = {4,3,6}>>> sset ([1, 2, 3, 4]) >>> Tset ([3, 4, 6]) >>> T | S  # and set Set ([1, 2, 3, 4, 6]) >>> T & S  #交集set ([3, 4]) >>> T-S  #差集 in T is not set in S ([[6]) >>& Gt t ^ s  #对称差集  value not present in TS set ([1, 2, 6])

5. Function-Type programming

    ①map    >>> a    [1, 2, 3, 4]    >>> n = map (lambda x:x+2,a)    >>> n    [3, 4, 5, 6]
    #注意: Requires n = List (n)    ②reduce    >>>reduce (Lambda x,y:x*y, Range (1,8)) in 3.x    #上述命令相当于    > >> s = 1    >>> for I in Range (1,8):    ...   s = S * I    ...     >>> s    5040    #注意: python3.x requires from fuctools import reduce    ③filter    >>> b = Filter ( Lambda x:x > 5 and X < 8, range (Ten))    >>> b    [6, 7]    #上述函数比for和while循环效率高

6. python2.x using Print ()

From __future___ import print_function

7. python2.x Division Change

>>> from __future__ Import Division
>>> 3/2
1.5
>>> 3//2
1

Python Learning (v): Easy to forget knowledge points

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.