Must-see 30 Python Language Features tips (3)

Source: Internet
Author: User
From the time I started learning Python, I decided to maintain a frequently used "tips" list. Whenever I see a paragraph that makes me feel "cool, it's OK!" "Code (in an example, in StackOverflow, in the open source software, and so on), I'll try it until I understand it and add it to the list. This article is part of the cleanup list. If you are an experienced Python programmer, though you may already know something, you can still find something you do not know. If you are a C, C + + or Java programmer who is learning python, or have just started to learn programming, you will find many of them very useful as I do.

Each trick or language feature can only be verified by an instance, without too much explanation. Although I have tried to make the examples clear, some of them will still look complicated, depending on how familiar you are. So if you're not sure if you've seen an example, the headline will provide you with enough information to get detailed content from Google.

Lists are sorted by difficulty, with common language features and techniques in front of them.

1.30 Maximum minimum elements (Heapq.nlargest and Heapq.nsmallest)

>>> a = [Random.randint (0, +) for in xrange (100)]

>>> Heapq.nsmallest (5, a)

[3, 3, 5, 6, 8]

>>> Heapq.nlargest (5, a)

[100, 100, 99, 98, 98]

1.31 Cartesian product (itertools.product)

>>> for P in Itertools.product ([1, 2, 3], [4, 5]):

(1, 4)

(1, 5)

(2, 4)

(2, 5)

(3, 4)

(3, 5)

>>> for P in Itertools.product ([0, 1], repeat=4):

... print '. Join (str (x) for x in P)

...

0000

0001

0010

0011

0100

0101

0110

0111

1000

1001

1010

1011

1100

1101

1110

1111

1.32 combination of combinations and permutations (itertools.combinations and Itertools.combinations_with_replacement)

>>> for C in Itertools.combinations ([1, 2, 3, 4, 5], 3):

... print '. Join (str (x) for x in C)

...

123

124

125

134

135

145

234

235

245

345

>>> for C in Itertools.combinations_with_replacement ([1, 2, 3], 2):

... print '. Join (str (x) for x in C)

...

11

12

13

22

23

33

1.33 Sort (itertools.permutations)

>>> for P in Itertools.permutations ([1, 2, 3, 4]):

... print '. Join (str (x) for x in P)

...

1234

1243

1324

1342

1423

1432

2134

2143

2314

2341

2413

2431

3124

3142

3214

3241

3412

3421

4123

4132

4213

4231

4312

4321

1.34 linked iterations (itertools.chain)

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

>>> for P in Itertools.chain (Itertools.combinations (A, 2), Itertools.combinations (A, 3)):

.. print P

...

(1, 2)

(1, 3)

(1, 4)

(2, 3)

(2, 4)

(3, 4)

(1, 2, 3)

(1, 2, 4)

(1, 3, 4)

(2, 3, 4)

>>> for subset in Itertools.chain.from_iterable (Itertools.combinations (A, n) for n in range (Len (a) + 1))

... print Subset

...

()

(1,)

(2,)

(3,)

(4,)

(1, 2)

(1, 3)

(1, 4)

(2, 3)

(2, 4)

(3, 4)

(1, 2, 3)

(1, 2, 4)

(1, 3, 4)

(2, 3, 4)

(1, 2, 3, 4)

1.35 grouping rows by given value (ITERTOOLS.GROUPBY)

>>> from operator Import Itemgetter

>>> Import Itertools

>>> with open (' Contactlenses.csv ', ' R ') as infile:

... data = [Line.strip (). Split (', ') for line in infile]

...

>>> data = data[1:]

>>> def print_data (rows):

... print ' \ n '. Join (' \ t '. Join (' {: <16} '. Format (s) for S-in row)

...

>>> Print_data (data)

Young Myope No reduced none

Young Myope no normal soft

Young Myope Yes reduced none

Young Myope Yes normal hard

Young Hypermetrope No reduced none

Young Hypermetrope no normal soft

Young Hypermetrope Yes reduced none

Young Hypermetrope Yes normal hard

Pre-presbyopic Myope No reduced none

Pre-presbyopic Myope no normal soft

Pre-presbyopic Myope Yes reduced none

Pre-presbyopic Myope Yes normal hard

Pre-presbyopic Hypermetrope No reduced none

Pre-presbyopic Hypermetrope no normal soft

Pre-presbyopic Hypermetrope Yes reduced none

Pre-presbyopic Hypermetrope Yes normal none

Presbyopic Myope No reduced none

Presbyopic Myope No normal none

Presbyopic Myope Yes reduced none

Presbyopic Myope Yes normal hard

Presbyopic Hypermetrope No reduced none

Presbyopic Hypermetrope no normal soft

Presbyopic Hypermetrope Yes reduced none

Presbyopic Hypermetrope Yes normal none

>>> Data.sort (Key=itemgetter (-1))

>>> for value, group in Itertools.groupby (data, Lambda r:r[-1]):

... print '-----------'

... print ' Group: ' + value

... print_data (group)

...

-----------

Group:hard

Young Myope Yes normal hard

Young Hypermetrope Yes normal hard

Pre-presbyopic Myope Yes normal hard

Presbyopic Myope Yes normal hard

-----------

Group:none

Young Myope No reduced none

Young Myope Yes reduced none

Young Hypermetrope No reduced none

Young Hypermetrope Yes reduced none

Pre-presbyopic Myope No reduced none

Pre-presbyopic Myope Yes reduced none

Pre-presbyopic Hypermetrope No reduced none

Pre-presbyopic Hypermetrope Yes reduced none

Pre-presbyopic Hypermetrope Yes normal none

Presbyopic Myope No reduced none

Presbyopic Myope No normal none

Presbyopic Myope Yes reduced none

Presbyopic Hypermetrope No reduced none

Presbyopic Hypermetrope Yes reduced none

Presbyopic Hypermetrope Yes normal none

-----------

Group:soft

Young Myope no normal soft

Young Hypermetrope no normal soft

Pre-presbyopic Myope no normal soft

Pre-presbyopic Hypermetrope no normal soft

Presbyopic Hypermetrope no normal soft

Related Article

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.