python_字典 list_4

>>> import string
>>> import random

#组合字符
>>> x=string.ascii_letters+string.digits+string.punctuation
>>> x
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
>>> y=[random.choice(x) for i in range(1000)]
>>> z=''.join(y)
>>> d=dict()

>>> z
'#OrW1%!MHoos%D\\|c$*H=JfcWf.=S23)PkU6!!XD3CT<X{$_F==$]%5Sb+M*>35#Z[of@imCm5Uz/<.T.@(nsWsLfNXT6z<Wdn1)Te6{]&^lHhY0{n!,fUe\\4h-z1cpl4\'I,L+3?\'5aBJf[PMhX*pe>i{t:poAc%5b*i@DN8D]f9G4\\)7l,>;Paui<moUTd"O5UVBb^r%WyS>Id\'yU3A>Nip3`!r*<N+1.`)#n&JjE\\#tT3H1\'CjjR{H>SF7"-w}->\\N*f@ce{)"L0996Q*6+qhrxo=l![Bl5IADWa\\F}K$Qi;e3:px`ShjbK0X|yjG\')691g4\\giQ(-a%g.U=MegLD"-8~oDzi{/eG/|j3-!DI?u(\'1Z[V/*<g~$-TAKy)@X\'d!|ZrX9S7#3[!X3-8Z\\@90XjtGLyE1a=^.tFQYE5)wt6FG3t{yAy|f6:["<df3cnbK9.eMT%XrjW$RdQ\\y&t1nhsPGnfh+9a@9Lv_*W_!OR8(l.S8KUNx9j<}^5iK:Bv&lb(Eg%[tj0410DBGj}:q(FZTG^.Qw}Z(Q=xVVx]PYZXdXciJLjT(:#/^P).TPJszyQm2GO+Obo\']^m&uVkP=P+v-;$d~\'#e*#m<3ib(MkF`yuz1Aain@u^T\\837`\'1L&e[`t)1]F-3;dur88#xO!05U!uN@5Qt.L1U~Jy\'@b7<RtzS&L-$9.uLT{a)g{oO;8x+[vkb0F<YOaaN2=4V.=rf,G=Pr2|s\\Nnq$&*i>>My0FkTn6p<c{V}pjz\'*f:XqNbANCSk2v\\%8K~z]1har:665"[3!_P%||e_:!vr1h+.9xWF<90M;_.[3VCUpX}j/Aqv|DnRZuQ,KM$?FfYEk9Qk5W$6O(XSrv/""@%@Rn($+Uo9F5G$HQV9/O31,}"Yz)!\\03Af)jMm-@Bc?`NDopNG/|kc+0iR:+Pga;/\\+Z++a3ELYo)")Ua\\\'IenrERX#^l<-9\'#$7k0\\bNyenHU5C4F(8"3JllF{N_tp|6'
>>> for ch in z:
d[ch]=d.get(ch,0)+1

>>> d
{'#': 11, '5': 15, 'Q': 12, 'z': 10, '<': 14, '-': 13, 'X': 16, 'H': 7, 'V': 9, 'o': 12, '1': 17, '"': 11, '/': 10, '!': 15, '4': 7, "'": 14, '}': 8, 'c': 10, '?': 4, ',': 6, 'd': 9, 'G': 12, '>': 9, 'U': 14, '$': 14, '8': 11, 'P': 12, 'a': 14, ']': 7, '_': 7, 'S': 10, 't': 12, '=': 12, 'l': 10, 'f': 15, '{': 12, '+': 15, 'F': 15, 'y': 13, 'Y': 7, 'b': 12, '^': 9, '@': 13, '0': 13, '.': 15, 'M': 10, 'x': 8, '9': 17, 'T': 14, 'W': 10, 'K': 8, 'B': 6, 'Z': 9, '3': 22, 'v': 8, 's': 6, 'J': 7, 'i': 14, 'A': 9, 'R': 8, '[': 11, 'L': 12, 'g': 8, 'D': 11, 'h': 9, '*': 12, '2': 5, 'N': 15, 'I': 5, 'n': 14, 'r': 13, '(': 12, 'q': 5, '|': 11, ';': 7, 'O': 10, 'u': 9, 'j': 15, '%': 11, 'k': 10, '7': 6, '~': 5, 'E': 7, '6': 13, 'C': 6, '&': 8, 'w': 3, ')': 15, '\\': 16, '`': 7, 'e': 13, 'p': 10, 'm': 7, ':': 10}

#方法二

>>> from collections import Counter
>>> frequences=Counter(z)
>>> frequences.items()

dict_items([('#', 8), ('h', 9), ('z', 19), (',', 9), ('=', 8), ('a', 9), ('Y', 11), ('X', 11), ('H', 10), ('V', 14), ('o', 8), ('"', 20), ('/', 11), ('{', 14), ('4', 9), ("'", 10), ('}', 10), ('c', 11), ('?', 16), ('t', 15), ('C', 6), ('d', 6), ('G', 15), ('>', 11), ('U', 11), ('$', 8), ('8', 11), ('P', 4), ('j', 11), (']', 7), ('_', 8), ('S', 6), ('e', 9), ('l', 8), ('f', 18), ('!', 12), ('+', 8), ('F', 10), ('y', 7), ('-', 9), ('b', 9), ('v', 12), ('@', 10), ('0', 11), ('.', 11), ('*', 9), ('M', 15), ('k', 6), ('T', 7), ('W', 11), ('K', 11), ('B', 15), ('Z', 12), ('3', 11), ('^', 6), ('s', 11), ('J', 17), ('i', 11), ('A', 9), ('R', 12), ('[', 12), ('L', 11), ('g', 18), ('D', 7), ('5', 12), ('<', 9), ('2', 11), ('N', 5), ('I', 14), ('n', 11), ('r', 12), ('(', 12), ('q', 14), ('|', 12), (';', 10), ('O', 9), ('u', 11), ('%', 10), (')', 7), ('7', 13), ('~', 12), ('E', 9), ('6', 10), ('Q', 7), ('&', 14), ('w', 12), ('x', 15), ('\\', 10), ('9', 10), ('`', 8), ('1', 7), ('p', 13), ('m', 9), (':', 11)])

>>> frequences.most_common(1)
[('"', 20)]
>>> frequences.most_common(3)
[('"', 20), ('z', 19), ('f', 18)]

上一篇:POJ 2305 Basic remains(进制转换)


下一篇:kbmmw 的远程桌面功能2-android手机端