Improved Python programming

Source: Internet
Author: User

1.capwords (s) function under the string library

It can remove whitespace from both ends, and then use a space instead of a continuous space character.

Itemgetter () function under 2.operator module, Itemgetter (item, ...)-Itemgetter object

The Itemgetter () parameter represents the index of an object that can be iterated, the number of which can vary depending on the number of objects that can be iterated.

Gameresult = [' Bob ', 95.00, ' a '],[' Alan ', 86.0, ' C '],[' Mandy ', 82.5, ' a '],[' Rob ',
, ' E ']

First output: Sort by rank, sort by score if same level

Second output: Sort by score, sort by rank when grades are the same

Third output: Sort by name, sort by score if name is the same

3. Using Defaultdict ()

From collections Import Defaultdict

my_dict=defaultdict (int)

Passing an int as the default value, equivalent to initialization, assuming that the key in the dictionary does not exist, does not error Keyerro, the default value is 0.

4. Use collections. Counter

The Counter class is a subclass of a dictionary class that has been added since Python2.7, and is a container object used primarily to count hash objects.

Support for collection operations + 、-、 &, |, where & and | The operation returns the minimum and maximum values for each element of the two Counter objects, respectively. It provides 3 different ways to initialize:
Counter ("Success") # Iterate over an object
Counter (s=3,c=2,e=1,u=1) # keyword parameter
Counter ({"S": 3, "C": 2, "U": 1, "E": 1}) # Dictionary

You can use the elements () method to get the key value in the Counter

Using the Most_common () method, you can find the top N most frequently occurring elements and their corresponding number of times.

When a nonexistent element is accessed, the default return is 0 instead of throwing the keyerror exception.

The update () method is used for updating the element of the statistic object, the original Counter counter object and the new element statistics
Count values are added instead of directly replacing them.


The subtract () method is used to subtract element statistics from the counter object, and the input and output statistic values are allowed to be 0 or
Negative number of the user.

Improved Python programming

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.