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