Today appreciate a solution that contains many points of knowledge. The topic is this:
Given two strings S1 and S2, count the individual lowercase letters they contain, and compare them to each other. If there are more occurrences of a letter in the S1, print "1:AAAA" (indicating that the letter a appears 4 times in the S1), print "2:AAAA" if the number of occurrences in the S2 occurs, or print "=:AAAA" if the number of times is the same. The final output of the entire comparison results, the output is sorted by the number of times, and then by 1-2-= sort, and finally alphabetically sorted.
As an example:
S1 = "My&friend&paul has heavy hats! & "
S2 = "My friend John has many many friends &"
Mix (s1, s2)--"2:nnnnn/1:aaaa/1:hhh/2:mmm/2:yyy/2:dd/2:ff/2:ii/2:rr/=:ee/=:ss/1:l/1:t/1:u/1:v/2:o"
Analysis:
This topic is actually quite simple. Count the number of letters? Traversal + dictionary easy to fix. Comparison of two dictionaries? You can do it individually. The last output of the first press ... Sort, then press ... Sort, and finally press ... Sort? It's a bit of a hassle, but it can be done with Itemgetter.
I refer to two methods and compare the performance of two solutions:
1 #-*-coding:utf-8-*-2 __author__='Administrator'3 4 fromCollectionsImportCounter5 ImportRe6 Import Time7 #S1 = "My&friend&paul has heavy hats! & "8 #s2 = "My friend John has many many friends &"9 #str_mix (S1, S2)--"2:nnnnn/1:aaaa/1:hhh/2:mmm/2:yyy/2:dd/2:ff/2:ii/2:rr/=:ee/=:ss/1:l/1:t/1:u/1:v/2:o" Ten defstr_min (S1,S2): Onec1=Counter (S1) AC2=Counter (S2) -D=[] -key1=C1.keys (); theKey2=C2.keys (); - - forKeyinchSet (List (Key1) +list (Key2)): - ifRe.match ("[A-z]", key): + ifC1[key]>C2[key]: -D.append (("1", Key,c1[key])) + elifc1[key]<C2[key]: AD.append (("2", Key,c2[key])) at Else: -D.append (("=", Key,c1[key])) - -rec=["{0}:{1}". Format (V,s*n) forV,s,ninchd] - returnRec - in defstr_min2 (S1,S2): -c1=Counter (Filter (STR.ISLOWER,S1)) toC2=Counter (Filter (STR.ISLOWER,S2)) +D=[] - forKeyinchSet (List (C1.keys ()) +list (C2.keys ())): then1=c1.get (key,0) *N2=c2.get (key,0) $ ifN1>0orN2>0:Panax Notoginseng ifN1>N2: -D.append (("1", key,n1)) the elifn1<N2: +D.append (("2", key,n2)) A Else: theD.append (("=", key,n1)) +rec=["{}:{}". Format (V,s*n) forV,s,ninchd] - returnRec $ $ if __name__=="__main__": -S1 ="My&friend&paul has heavy hats! &" -S2 ="my friend John has many many friends &" thet0=time.time () - forIinchRange (100000):WuyiRec=str_min (S1,S2) the Print('/'. Join (Sorted (rec,key=LambdaS: (-Len (s), s) ))) -T1=time.time ()-T0 Wu Print(t1) - Print("-"*100) Aboutt0=time.time () $ forIinchRange (100000): -Rec=str_min2 (S1,S2) - Print('/'. Join (Sorted (rec,key=LambdaS: (-Len (s), s) ))) -T1=time.time ()-T0 A Print(t1) + the
View Code
Output:
2:nnnnn/1:aaaa/1:hhh/2:mmm/2:yyy/2:dd/2:ff/2:ii/2:rr/=:ee/=:ss/1:l/1:t/1:u/1:v/2:o
14.07480502128601
----------------------------------------------------------------------------------------------------
2:nnnnn/1:aaaa/1:hhh/2:mmm/2:yyy/2:dd/2:ff/2:ii/2:rr/=:ee/=:ss/1:l/1:t/1:u/1:v/2:o
8.899508953094482
Algorithm: Several important functions are applied to a topic