Python string manipulation

Source: Internet
Author: User

Today do Newcode ' histogram within the maximum rectangle ' problem, the pit daddy string operation toss a long time, specially to summarize under. Topic for (http://www.nowcoder.com/practice/13ba51c3fec74b58bbc8fa8c3eedf877?tpId=49&tqId=29284&rp=1&ru=/ ta/2016test&qru=/ta/2016test/question-ranking)

First put the code, the results of this machine Python3.4 run no problem, Newcoder is Python2.7, error, not resolved:%d format:a number is required, not nonetype

#-*-coding:utf-8-*-classMaxinnerrec:defCountarea (self, A, n):#Write code hereL = []         forIiinchRange (n-1, 0, 1):             forJjinchRange (0, ii):ifA[JJ] > A[JJ + 1]:                    #tmp = A[JJ]                    #A[JJ] = a[jj + 1]                    #A[JJ + 1] = A[jj]A[JJ],A[JJ + 1] = A[JJ + 1],A[JJ]PrintA#after sorting is complete         forIinchrange (N): L.append (A[i]* (N-i))#Calculate Area         forIiinchrange (n):ifL[0] <L[ii]: l[0], L[ii]=L[ii], l[0]Print(l)#Sort an area        Print(l[0])if __name__=="__main__": A=[] SS=input () AA= Ss.replace ('[',' ') AA= Aa.replace (']',' ') AA= Aa.split (',') n= Int (aa[-1])     forIinchRange (int (aa[-1]): a.append (int (aa[i) ))Print(A)#A = [2,7,9,4,1]    #n = 5CH=Maxinnerrec () Ch.countarea (a,n)


Body: string manipulation

Replace: string substitution
SS = [2,7,9,4,1= ss.replace ('[','') #SS String All ' [' Use ' the ' space instead of AA =  2,7,9,4,1]
Split: String splitting
    A = ' 2,7,9,4,1 '      #A is a string    of    AA = aa.split (',')    # a to make a a string type of list
When split has multiple delimiters, it is necessary to use regular expressions:
Importres1= [8,4,6,2,5,9,1],7S2=re.split (R'[\[\s\]\,]+', S1)#remove [, Space,],+ to represent multiple symbolsPrint(S2) ["','8','4','6','2','5','9','1','7']#Why is there a space in front of you that can't go away

Join: String connection
A = ['a','b','c',' D ' ]     ". Join (a)print  contentabcd
Add again next time:

Python string manipulation

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.