Features and skills of 30 Python languages (1)

Source: Internet
Author: User
Each trick or language feature can only be verified by an instance, without too much explanation. Although I have tried my best to make the examples clear, some of them still seem complicated, depending on your familiarity. So if you still don't know the example, I decided to maintain a frequently used "tip" list from the beginning when I learned Python. Whenever I see a piece that makes me feel "cool, that's okay !" (In an example, in StackOverflow, in open source software, etc.), I will try it until I understand it and then add it to the list. This article is part of the list. If you are an experienced Python programmer, although you may already know something, you can still find something you don't know. If you are a C, C ++, or Java programmer learning Python, or you are learning programming at the beginning, you will find many of them very useful like me.

Each trick or language feature can only be verified by an instance, without too much explanation. Although I have tried my best to make the examples clear, some of them still seem complicated, depending on your familiarity. Therefore, if you are not clear about the example, the title can provide enough information for you to obtain detailed content through Google.

The list is sorted by difficulty. Common language features and skills are placed at the beginning.

1.1 Split

>>> 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]

>>>

1

>>> B

2

>>> C

3

>>> D

4

1.2 swap variable splitting

>>> A, B = 1, 2

>>> A, B = B,

>>> A, B

(2, 1)

1.3 Expansion split (applicable to Python 3)

>>> A, * B, c = [1, 2, 3, 4, 5]

>>>

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 List slice (a [start: end])

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

>>> A [2: 8]

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

1.6 list slices using negative indexes

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

>>> A [-4:-2]

[7, 8]

1.7 List slices with step values (a [start: end: step])

>>> 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, 4, 6]

1.8 negative step worthy list slice

>>> 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 slice assignment

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

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

>>>

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

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

>>>

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

>>> A [1:-1] = []

>>>

[1, 5]

1.10 named slice (start, end, step ))

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

>>> LASTTHREE = slice (-3, None)

>>> LASTTHREE

Slice (-3, None, None)

>>> A [LASTTHREE]

[3, 4, 5]

1.11 zip package unpacking list and multiples

>>> 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 merge adjacent list items using zip

>>> 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 use zip and iterators to generate a sliding window (n-grams)

>>> From itertools import islice

>>> Def n_grams (a, n ):

... Z = (islice (a, I, None) 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 use zip reverse dictionary

>>> 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 '}

The above is a detailed description of the features and skills of the 30 Python languages (1). For more information, see other related articles in the first PHP community!

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.