21 Tips for Python

Source: Internet
Author: User
from the beginning of my study python , I began to summarize a python a collection of small tricks. And then when I was in Stack Overflow

Or when I saw a cool piece of code in an open-source software, I was surprised that I could do it! , I tried to try this code myself, until I understood the whole idea, and I added the code to my collection. This blog is actually part of the public appearance of this collection after finishing. If you are already a Python Daniel, then basically you should know most of the usage, but I think you should also be able to find some new tricks you don't know. And if you were a C,c++,java programmer, learning python at the same time, or simply being a novice at just learning to program, you should see a lot of practical tricks that are especially useful to surprise you, just like I did.

Every skill and language usage is presented to you in one instance, and no other explanation is required. I've tried to make every example easy to understand, but because of the reader's familiarity with Python, there are still some obscure areas that may be unavoidable. So if these examples don't make you understand, at least the title of this example will help you when you go to Google search behind you.

The whole set is probably sorted by the difficulty level, simple and common in the front, relatively rare in the last.

1.1 Unpacking

>>> A, B, C = 1, 2, 3

>>> A, B, c

(1, 2, 3)

>>> A, b, C = [1, 2, 3]

>>> A, B, c

(1, 2, 3)

>>> A, B, C = (2 * i + 1 for I in range (3))

>>> A, B, c

(1, 3, 5)

>>> A, (b, c), d = [1, (2, 3), 4]

>>> A

1

>>> b

2

>>> C

3

>>> D

4

1.2 Split-Box variable Exchange

>>> A, B = 1, 2

>>> A, B = B, a

>>> A, b

(2, 1)

1.3 Expansion unboxing (compatible with Python3 only)

>>> A, *b, C = [1, 2, 3, 4, 5]

>>> A

1

>>> b

[2, 3, 4]

>>> C

5

1.4 Negative Index

>>> a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

>>> A[-1]

10

>>> A[-3]

8

1.5 Cut List

>>> a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

>>> A[2:8]

[2, 3, 4, 5, 6, 7]

1.6 Negative Index cut list

>>> a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

>>> A[-4:-2]

[7, 8]

1.7 Specifying the Step cut list

>>> a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

>>> A[::2]

[0, 2, 4, 6, 8, 10]

>>> A[::3]

[0, 3, 6, 9]

>>> A[2:8:2]

[2, 4, 6]

1.8 Negative Step cut list

>>> a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

>>> A[::-1]

[10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

>>> A[::-2]

[10, 8, 6, 4, 2, 0]

1.9 List Cut Assignment

>>> a = [1, 2, 3, 4, 5]

>>> A[2:3] = [0, 0]

>>> A

[1, 2, 0, 0, 4, 5]

>>> a[1:1] = [8, 9]

>>> A

[1, 8, 9, 2, 0, 0, 4, 5]

>>> a[1:-1] = []

>>> A

[1, 5]

1.10 Named List Cutting method

>>> a = [0, 1, 2, 3, 4, 5]

>>> Lastthree = Slice ( -3, None)

>>> Lastthree

Slice ( -3, none, none)

>>> A[lastthree]

[3, 4, 5]

1.11 List and the compression and decompression of iterators

>>> a = [1, 2, 3]

>>> B = [' A ', ' B ', ' C ']

>>> z = Zip (A, B)

>>> Z

[(1, ' a '), (2, ' B '), (3, ' C ')]

>>> Zip (*z)

[(1, 2, 3), (' A ', ' B ', ' C ')]

1.12 List of adjacent element compressors

>>> a = [1, 2, 3, 4, 5, 6]

>>> Zip (* ([ITER (a)] * 2))

[(1, 2), (3, 4), (5, 6)]

>>> group_adjacent = lambda A, k:zip (* ([ITER (a)] * k))

>>> Group_adjacent (A, 3)

[(1, 2, 3), (4, 5, 6)]

>>> Group_adjacent (A, 2)

[(1, 2), (3, 4), (5, 6)]

>>> Group_adjacent (A, 1)

[(1,), (2,), (3,), (4,), (5,), (6,)]

>>> Zip (A[::2], a[1::2])

[(1, 2), (3, 4), (5, 6)]

>>> Zip (A[::3], A[1::3], A[2::3])

[(1, 2, 3), (4, 5, 6)]

>>> group_adjacent = lambda A, k:zip (* (A[i::k] for I in range (k)))

>>> Group_adjacent (A, 3)

[(1, 2, 3), (4, 5, 6)]

>>> Group_adjacent (A, 2)

[(1, 2), (3, 4), (5, 6)]

>>> Group_adjacent (A, 1)

[(1,), (2,), (3,), (4,), (5,), (6,)]

1.13 Sliding the value window with a compressor and an iterator in the list

>>> def n_grams (A, N):

... z = [iter (a[i:]) for I in range (n)]

... return zip (*z)

...

>>> a = [1, 2, 3, 4, 5, 6]

>>> N_grams (A, 3)

[(1, 2, 3), (2, 3, 4), (3, 4, 5), (4, 5, 6)]

>>> N_grams (A, 2)

[(1, 2), (2, 3), (3, 4), (4, 5), (5, 6)]

>>> N_grams (A, 4)

[(1, 2, 3, 4), (2, 3, 4, 5), (3, 4, 5, 6)]

1.14 Reversing a dictionary with a compressor

>>> m = {' A ': 1, ' B ': 2, ' C ': 3, ' d ': 4}

>>> M.items ()

[(' A ', 1), (' C ', 3), (' B ', 2), (' d ', 4)]

>>> Zip (m.values (), M.keys ())

[(1, ' a '), (3, ' C '), (2, ' B '), (4, ' d ')]

>>> mi = dict (Zip (m.values (), M.keys ()))

>>> mi

{1: ' A ', 2: ' B ', 3: ' C ', 4: ' d '}

1.15 List Expansion

>>> a = [[1, 2], [3, 4], [5, 6]]

>>> List (itertools.chain.from_iterable (a))

[1, 2, 3, 4, 5, 6]

>>> sum (A, [])

[1, 2, 3, 4, 5, 6]

>>> [x for L in a for x in L]

[1, 2, 3, 4, 5, 6]

>>> a = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]

>>> [x for L1 in a for L2 in L1 for X in L2]

[1, 2, 3, 4, 5, 6, 7, 8]

>>> a = [1, 2, [3, 4], [[5, 6], [7, 8]]

>>> flatten = lambda x: [y for L in X for y in Flatten (l)] if type (x) is list else [x]

>>> Flatten (a)

[1, 2, 3, 4, 5, 6, 7, 8]

1.16 Generator Expression

>>> g = (x * * 2 for X in Xrange (10))

>>> Next (g)

0

>>> Next (g)

1

>>> Next (g)

4

>>> Next (g)

9

>>> SUM (x * * 3 for X in Xrange (10))

2025

>>> SUM (x * * 3 for X in xrange () if x 3 = = 1)

408

1.17 Dictionary Derivation

>>> m = {x:x * * 2 for X in range (5)}

>>> m

{0:0, 1:1, 2:4, 3:9, 4:16}

>>> m = {x: ' A ' + str (x) for X in range (10)}

>>> m

{0: ' A0 ', 1: ' A1 ', 2: ' A2 ', 3: ' A3 ', 4: ' A4 ', 5: ' A5 ', 6: ' A6 ', 7: ' A7 ', 8: ' A8 ', 9: ' A9 '}

1.18 Derivation of inverse dictionaries using dictionaries

>>> m = {' A ': 1, ' B ': 2, ' C ': 3, ' d ': 4}

>>> m

{' d ': 4, ' A ': 1, ' B ': 2, ' C ': 3}

>>> {v:k for K, V in M.items ()}

{1: ' A ', 2: ' B ', 3: ' C ', 4: ' d '}

1.19 named tuples

>>> point = collections.namedtuple (' point ', [' X ', ' Y '])

>>> p = Point (x=1.0, y=2.0)

>>> P

Point (x=1.0, y=2.0)

>>> p.x

1.0

>>> p.y

2.0

1.20 inheriting named tuples

>>> class Point (Collections.namedtuple (' pointbase ', [' X ', ' Y ']):

... __slots__ = ()

... def __add__ (self, Other):

... return point (x=self.x + other.x, Y=self.y + other.y)

...

>>> p = Point (x=1.0, y=2.0)

>>> q = Point (x=2.0, y=3.0)

>>> p + q

Point (x=3.0, y=5.0)

1.21 operation set

>>> A = {1, 2, 3, 3}

>>> A

Set ([1, 2, 3])

>>> B = {3, 4, 5, 6, 7}

>>> B

Set ([3, 4, 5, 6, 7])

>>> A | B

Set ([1, 2, 3, 4, 5, 6,

Free pick up the Lamp Brothers and original PHP video tutorial CD/"PHP" Essentials Edition, details of the Advisory website customer Service: http://www.lampbrother.net

Phpcms two times Development http://yun.itxdl.cn/online/phpcms/index.php?u=5

Development http://yun.itxdl.cn/online/weixin/index.php?u=5

Mobile Internet server-side development http://yun.itxdl.cn/online/server/index.php?u=5

JavaScript Course http://yun.itxdl.cn/online/js/index.php?u=5

CTO Training Camp Http://yun.itxdl.cn/online/cto/index.php?u=5

Here are 21 tips on Python, including aspects of the content, hoping to be interested in the PHP tutorial friends helpful.

  • 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.