Improve your Python code (9) __python

Source: Internet
Author: User
1. String Handling Summary
#!/usr/bin/env python # encoding:utf-8 "" "@python version:python3.6.1 @author: Xiangguosun @contact: Sunxiangguodut@q
Q.com @site: http://blog.csdn.net/github_36326955 @software: pycharm @file: strmethod.py @time: 5/7/2017 12:45 PM "" "" "" "" "  The string operation "" "" s = "I like the Tian ' anmen Square of the capital Beijing."
"Print (S.count (')") # Output:2 print ("Beijing" in s) # output:true res = s.replace ('. ', '! ')   Print (S, "|", res) "" "Output:i like the Tian ' anmen Square of the capital Beijing.     |
I like the Tian ' anmen Square of the capital beijing! "" "Pre, key, post = S.partition (" Tian ' anmen Square ") print (Pre," | ", Key," | ", Post)" "" Output:i like the |     Tian ' Anmen Square |
The capital Beijing. "" "Print (S.split ()) Print (S.split (" "))" "Output: [' I ', ' like ', ' the ', ' Tian ', ' anmen ', ' Square ', ' of ', ' ', ', '
' Beijing. '] [', ', ', ' ', ' I ', ' like ', ', ', ', ', ' Tian ' anmen ', ' Square ', ', ', ', ', ', ', ', ', ', ', ', ', '"" "" Print (S.title ()), import string print (String.capwords (s)) "" "Output:i like the Tian ' anmen Square of the
Capital Beijing.
I like the Tian ' anmen Square of the capital Beijing. "" "Word_list = S.split () print (centered:) for word in Word_list:print (word.center) print (" left Justified ") for word in Word_ List:print (word.ljust) print ("0 fill") for word in Word_list:print (Word.zfill (5)) "" Output: Center: I Lik
E The Tian ' anmen Square of the capital Beijing.
Left-aligned I like the Tian ' anmen Square of the capital Beijing.
0 Fill 0000I 0like 00the Tian ' anmen Square 000of capital Beijing.
"" "Print (s) print (S.strip ())" "" Output:i like the Tian ' anmen Square of the capital Beijing.
I like the Tian ' anmen Square of the capital Beijing.
"" "Print (s) print (" ". Join (S.split ()))" "" Output:i like the Tian ' anmen Square of the capital Beijing.
I like the Tian ' anmen Square of the capital Beijing. """
2.operator.itemgetter function

The Itemgetter function provided by the operator module is used to get the dimension data of the object, with some ordinal number (that is, the ordinal number of the data to be obtained in the object), if you have used Padas, numpy, I believe you will understand the concept. Equivalent to Axis there. Look at the example below.

A = [1,2,3] 
b=operator.itemgetter (1)     #定义函数b, gets the 1th domain of the object,
B (a) 
2 
b=operator.itemgetter (1,0)   #定义函数b, gets the 1th field and No. 0 value of the object
B (a) 
(2, 1)

Multilevel sorting with operator function

Students = [(' John ', ' A ',], (' Jane ', ' B ', '), (' Dave ', ' B ',],]  
sorted (students, key=itemgetter (1,2)) ""
Ort by grade then by age ""  
[(' John ', ' A ',], (' Dave ', ' B ', "," (' Jane ', ' B ', 12)]  

Sort the dictionary

D = {' Data1 ': 3, ' data2 ': 1, ' data3 ': 2, ' DATA4 ': 4}  
sorted (D.iteritems (), Key=itemgetter (1), reverse=true)  
[ Data4 ', 4), (' Data1 ', 3), (' Data3 ', 2), (' Data2 ', 1)]
3. Sort and sorted
persons = [{' Name ': ' Bon ', ' Age ': #}, {' name ': ' Alan ', ' Age ':}, {' name ': ' Bon ', ' Age ': 33}, {' Name ': ' Job ', ' age ':] ' "' Sort and sorted prototype: Sorted (Iterable[,cmp[,key[,reverse]]]) s.sort ([Cmp[,key[,reverse]]]) CMP: Comparison function key: A function with one parameter that extracts the comparison value for each element, by default, none Reverse: Indicates whether the result is reversed "" "" "and the first order of name is small to large. For the same name, the small to large sort "" "Print (sorted (persons, Key = lambda x: (x[' name '],x[' age ')) print (persons) Persons.sort (key = lambda x: (x[' name '],x[' age ')) print (persons) print (Persons.sort (key = lambda x: (x[' name '],x[' age ')) "" "Output [{] Name ': ' Alan ', ' age ': {' name ': ' Bon ', ' Age ': #}, {' name ': ' Bon ', ' age ': ' +} ', {' name ': ' Job ', ' Age ': '} ' [{' name ']: ' Bon ', ' Age ':}, {' name ': ' Alan ', ' Age ': +}, {' name ': ' Bon ', ' age ': ', {' name ': ' Job ', ' Age ': '} ' [{' name ']: ' Alan ', ' Age ':}, {' name ': ' Bon ', ' age ': +}, {' name ': ' Bon ', ' age ': +}, {' name ': ' Job ', ' age ': '] None sorted will return a sorted list, the original
The list is unchanged and sort modifies the existing list directly, and the function returns none. The "" "" Sorted () acts on any iteration object, and sort generally acts on the list: "" a_tUple = (1,2,4,2,3) # Print (A_tuple.sort ()) # Error print (sorted (a_tuple)) # correct [1, 2, 2, 3, 4] from operator import item Getter "" "Sort Dictionary:" "" phonebook = {' Linda ': ' 7750 ', ' Bob ': ' 9345 ', ' Carol ': ' 5834 '} SORTED_PB = sorted (phonebook, Key= Itemgetter (1)) # sorted by number size print (SORTED_PB) "" "," "Multidimensional list:" "Gameresult = [[' Bob ', km, ' A '],[' Alan ',], ' C '],[' Mandy ', 82
, ' A '],[' Rob ', the ' E '] ' Print (Sorted (Gameresult,key=itemgetter (2,1)) from operator import Itemgetter "" Dictionary mixed List ""
         Mydic = {' Li ': [' m ', 7], ' Lh ': [' m ', 6], ' Zhang ': [' E ', 2], ' Wang ': [' P ', 3], ' Du ': [' C ', 9], ' Ma ': [' C ', 2], ' Zhe ': [' H ', 7]} ' "' Below k= (' Li ': [' M ', 7])" "" Print (Sorted (Mydic.items (), Key=itemgetter (0) ) # follow ' Li ' sort print (sorted (Mydic.items (), Key=lambda k:itemgetter (1) (k[1))) # According to 7 sort print (sorted (mydic.items), key=  Lambda k:itemgetter (0) (k[1])) # According to ' M ' sort ' "' Output: [(' Du ', [' C ', 9]), (' Lh ', [' m ', 6]), (' Li ', [' m ', 7]), (' Ma ', [' C '], 2]), (' Wang ', [' P ', 3]), (' Zhang ', [' E ', 2]), (' Zhe ', [' H ', 7])] [(' Zhang ', [' E '], 2]), (' Ma ', [' C ', 2]), (' Wang ', [' P ', 3]), (' Lh ', [' m ', 6]), (' Li ', [' m ', 7] ), (' Zhe ', [' H ', 7]), (' Du ', [' C ', 9])] [(' Du ', [' C ', 9]], (' Ma ', [' C ', 2]), (' Zhang ', [' E ', 2]), (' Zhe ', [' H ', 7]), (' Li ' , [' m ', 7]), (' Lh ', [' m ', 6]), (' Wang ', [' P ', 3]] "" "" "" "," "" "" game = [{' name ']: ' A ', ' Grade ': 2},{' name ': ' B ', ' GRA 










 De ': 1}] Print (Sorted (game,key=itemgetter (' Grade ')) "" "Output:[{' name ': ' B ', ' Grade ': 1}, {' name ': ' A ', ' Grade ': 2}]" ""

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.