python随机生成1000个字母,数字,符号的字符串,并统计每个字符串出现的次数

>>> import string
>>> import random
>>> x = string.ascii_letters + string.digits + string.punctuation
>>> y = [random.choice(x) for i in range(1000)]
>>> z = ''.join(y)
>>> d = dict()    #创建空字典
>>> for ch in z:    #统计每个字符在字符串中出现的次数
 d[ch] = d.get(ch, 0) + 1
 
>>> d    #每个字符出现的次数
{'B': 11, '<': 7, 'o': 11, 'Y': 12, 'd': 11, 'v': 12, '8': 8, 'P': 6, 'h': 10, '>': 20, 't': 14, ']': 11, '~': 12, '`': 7, 'w': 14, 's': 10, '7': 8, '%': 7, "'": 16, '9': 9, 'p': 9, 'j': 13, 'I': 14, 'c': 10, 'F': 7, '/': 6, 'i': 12, '|': 6, 'f': 12, 'H': 9, '}': 11, '_': 12, '$': 15, 'r': 13, 'k': 5, 'y': 9, '*': 9, '0': 11, 'W': 9, '\\': 14, ',': 13, '{': 16, 'q': 13, '6': 9, '3': 10, '@': 12, 'm': 11, 'Z': 11, '+': 14, 'N': 9, '=': 9, 'x': 10, '^': 15, 'S': 11, 'O': 8, '1': 8, 'D': 17, 'e': 11, '&': 14, 'a': 9, 'K': 9, 'U': 19, '.': 11, 'G': 9, '5': 10, 'V': 13, '#': 14, 'E': 10, '4': 9, 'R': 13, 'X': 8, 'g': 12, '!': 8, 'n': 11, 'L': 10, 'Q': 10, 'l': 7, 'u': 13, ';': 14, '2': 10, '(': 6, ':': 10, 'T': 10, 'J': 9, 'M': 8, '[': 9, '"': 9, 'z': 7, '?': 9, 'b': 10, 'A': 12, ')': 11, 'C': 11, '-': 7}
>>> z    #字符串的实际情况如下所示
'Bvot]v~`ws7%`\'9pjYI~cF/>i|ifH|}]_$rky*0W\\t,{F\\qW76I3@rmZ+tfN=x@w^x`SO\\1D@rW\\e~&aKU}q.tG.79O5rO}&V>p#E646qa}D$w}Ri5XgI!o^g\'rmBI|eGnvWVY\'_x]LyfQ9R0hh{>RsWi.f.D\'WUBRlQu8y]l7\';L^DS1#k#v+X2;@>D(L&H\'vn:Q9$TFJdDGSl{Esq(/JM>[f86,htP"d3zm#?EF$q>H{_4zl\\IRbog>ZeM_K_4i/{B%.PKd(DpAc&gsN,PO0$FU##>b3R;,@@UE1NP_{j\'o)t~AxD0Sd~HVy.lwrDI&h*rZntT\'L9}qupQbhB>;8C)w*kwf/%K30R4CEU2Q)v>>jX|qi,,yEBTZL^ZKl{9#wndA<;+bZI)S1Yf)S[x:iIB-a=*5\\#?"]-EUm.5p2+e!eK:{fhcNUvU{Db+:4kjtNrcYj_`;Q,w?s:j~X?$\'YcJbHR~)E^"ITTG1!*^Mu3l?Sw.Z&B2;pd""St\\4_-r\\{&yLG5A}#oV{2@VDu?jx\\^z3ng#qN\'U;&55M/u|uZKMU~o${*cvrzi+#ezu0=S7KLCr0)d9_8;"%wmgL~4^Ia[Hn%$m`f:Q[R=oIw&q\\^%cHjJTXA7,Wc\'58`giEtcA}*]G;n3B2a[+=g2$@3O-\\#^Z}T)2gn]e2J)8h`F9$Acv[ab+Z>+*sjg3f=dNC3mU+>j6"!g\'^X"Uw.~AaY/}ZM;Yy@wvJG<.$CjyYNhpiYh%:^t#LQ,Uv0u-q9Q;1btdVF1;RIV64>\\7$U~gCo-:5Ue0]J+quxom,RJ(${fT\\nBLV{Ut[HV=\\+V0:C6(8&JCT6;eAfu[xsXYOnC\'=p#Au>Gk.TVS,PiA?_@,m>]+!IsQd_!jS\']y-"&1qsb!^e@Cj]*4HEp7$R\'@[5z(YUrbD\'{uu2w^a_DD!tqCI=&ODaeMGoKm'
>>> len(z)
1000
>>> len(d)
94


也可以通过 collections模块的defaultdict类来实现统计字符串中字符出现的次数
>>> import string
>>> import random
>>> x = string.ascii_letters + string.digits + string.punctuation
>>> y = [random.choice(x) for i in range(1000)]
>>> z = ''.join(y)
>>> from collections import defaultdict
>>> frequences = defaultdict(int)
>>> frequences
defaultdict(, {})
>>> for item in z:
 frequences[item] = frequences[item] + 1
 
>>> frequences.items()
dict_items([('}', 16), (';', 11), (':', 14), ('!', 7), ('.', 9), ('N', 17), ('j', 14), ('d', 10), ('K', 11), ('~', 7), ('(', 11), ('Z', 10), ('l', 15), ('|', 10), ('_', 7), ('v', 15), ('w', 12), ('?', 10), ('D', 7), ('*', 9), (',', 11), ('i', 16), ('r', 7), ('k', 9), ('E', 10), ('m', 15), ('g', 11), ('G', 10), ('q', 10), ('Q', 14), ('"', 11), ('M', 8), ('F', 11), ('e', 9), ('t', 15), ('x', 9), ('&', 7), ('{', 12), ('H', 12), ('B', 11), ('V', 11), ('6', 11), ('C', 10), ('\\', 13), ('L', 17), ('f', 12), ('1', 10), ('3', 8), ('0', 15), ('W', 6), ('[', 10), ('5', 8), ('Y', 13), ('u', 12), ('z', 13), ('c', 7), ('O', 13), ('n', 8), ('b', 8), ('p', 11), ('4', 10), ('U', 11), ('h', 13), ('/', 12), ('X', 11), ('%', 5), ('>', 10), ('=', 8), ('+', 10), ('P', 9), ('J', 10), ('R', 11), ('#', 7), ('-', 12), ('s', 7), (']', 10), ('$', 13), ('T', 12), ('I', 16), ('o', 9), ('9', 10), ('a', 7), ("'", 16), ('2', 11), ('y', 10), (')', 10), ('8', 5), ('A', 10), ('7', 16), ('^', 11), ('@', 8), ('S', 10), ('`', 9), ('<', 5)])
>>> z
'};:!.NjdK~(Zl|_vw?:wD*,jirkEmgGqQ"M*Fetx&{HdwDB}!Vii6C|,\\L,}f.(11360Wv[lQ5Yuzuxf!c}\\?~Onumzb3VFp4~UcZNhLxl/uXGK%zif>=+"XUguPfvJpRlO\\[:#RzHl*k-s(.Jm6}]$$g6KF\\T$Iwko:r=>]XHxV4_w9aa\'B-/2j{NtCK-|w+wJyv5[V4\\1l|F5K!Q!(mbjOF~Y)86Nz\\HR.T[:\'C*Rc-jrZ\'1?,L)>:fuHabI~L-iMumQ;RK&).Q%vA3L4d78b\'OlM.sF2UEp?(iijN\'NL+,:[EvIAJT+0Nc9:7z%0lJ)h,xBIhz?#R^z\\W@"SU{-LiLXv`}-^^l13j4l}BIL0Xg^`le:@A^k:0(PUsIRwTb2)Fx;5:@6\\7t:^<]p;b/Raj7a`zfjIW^Z8Gd69TA/=\\1&Y]sL?0"]lE.0mkqNyeg,fjMbP7t@Q9/|hYhdr{_KKg-p$2(EYwP2Qu7LmFi^/Vz^qgGRh;jK`9tCe>N+6[QE~txt7\'>YUP<)pj(RwQnJnGl*$]t3$=<223{kNh/v>=56?}-~{q)T#w2VGX2:U9FHd\\y=dQ+/}Xo$;QZ4yyY#v^NCXHX$;X{{MN0Jr_VEk_ZU5[#;tY>WSShEO3Lff/*Z7td`\'{T.rzgh\'yLRI}oBqqSnv*v7N]0xC032I}@fe&"H^O"GF\\HAB)LY|?ijQMp"]f&(ed60Sh8D"mMyqOUBE1OYEy>I\\hO,!&iD-s((L7S1QK/4;MuIvmN/C\'W7"5019%0IYvY[>ek$Oom'
>>> len(z)
1000

使用collections模块的Counter类可以快速实现这个功能,并且提供更多功能,例如查找出现次数最多的元素。
>>> from collections import Counter
>>> frequences = Counter(z)
>>> frequences.items()
dict_items([('}', 16), (';', 11), (':', 14), ('!', 7), ('.', 9), ('N', 17), ('j', 14), ('d', 10), ('K', 11), ('~', 7), ('(', 11), ('Z', 10), ('l', 15), ('|', 10), ('_', 7), ('v', 15), ('w', 12), ('?', 10), ('D', 7), ('*', 9), (',', 11), ('i', 16), ('r', 7), ('k', 9), ('E', 10), ('m', 15), ('g', 11), ('G', 10), ('q', 10), ('Q', 14), ('"', 11), ('M', 8), ('F', 11), ('e', 9), ('t', 15), ('x', 9), ('&', 7), ('{', 12), ('H', 12), ('B', 11), ('V', 11), ('6', 11), ('C', 10), ('\\', 13), ('L', 17), ('f', 12), ('1', 10), ('3', 8), ('0', 15), ('W', 6), ('[', 10), ('5', 8), ('Y', 13), ('u', 12), ('z', 13), ('c', 7), ('O', 13), ('n', 8), ('b', 8), ('p', 11), ('4', 10), ('U', 11), ('h', 13), ('/', 12), ('X', 11), ('%', 5), ('>', 10), ('=', 8), ('+', 10), ('P', 9), ('J', 10), ('R', 11), ('#', 7), ('-', 12), ('s', 7), (']', 10), ('$', 13), ('T', 12), ('I', 16), ('o', 9), ('9', 10), ('a', 7), ("'", 16), ('2', 11), ('y', 10), (')', 10), ('8', 5), ('A', 10), ('7', 16), ('^', 11), ('@', 8), ('S', 10), ('`', 9), ('<', 5)])
>>> frequences.most_common(1)    #统计出现次数第一的字符和对应的出现次数
[('N', 17)]
>>> frequences.most_common(3)    #统计出现次数排名前三的字符和对应的次数
[('N', 17), ('L', 17), ('}', 16)]

你可能感兴趣的:(各种案例分析)