弦分析器

时间:2019-02-02 05:36:09

标签: python

用pyhton编码的最短方法是什么?我可以在可检查和计数输入字符串的循环中包含计数器吗?

from collections import Counter

    ans = 'y'
    while ans == 'y' or ans == 'Y':
        print 'String Analyzer ver 1.0'

        string = raw_input('Enter a String: ')

        counter = Counter(string)

        counter['a'],counter['A'],counter['b'],counter['B'],counter['c'],counter['C'],counter['d'],counter['D'],counter['e'],counter['E'],counter['f'],counter['F']
        counter['g'],counter['G'],counter['h'],counter['H'],counter['i'],counter['I'],counter['j'],counter['J'],counter['k'],counter['K'],counter['l'],counter['L']
        counter['m'],counter['M'],counter['n'],counter['N'],counter['o'],counter['O'],counter['p'],counter['P'],counter['q'],counter['Q'],counter['r'],counter['R']
        counter['s'],counter['S'],counter['t'],counter['T'],counter['u'],counter['U'],counter['v'],counter['V'],counter['w'],counter['W'],counter['x'],counter['X']
        counter['y'],counter['Y'],counter['z'],counter['Z']
        letterU = counter['A'] + counter['B'] + counter['C'] + counter['D'] + counter['E'] + counter['F'] + counter['G'] + counter['H'] + counter['I'] + counter['J']+ counter['K'] + counter['L'] + counter['M'] + counter['N'] + counter['O'] + counter['P'] + counter['Q'] + counter['R'] + counter['S'] + counter['T'] + counter['U']+ counter['V'] + counter['W'] + counter['X'] + counter['Y'] + counter['Z']
        letterL = counter['a'] + counter['b'] + counter['c'] + counter['d'] + counter['e'] + counter['f'] + counter['g'] + counter['h'] + counter['i'] + counter['j']+ counter['k'] + counter['l'] + counter['m'] + counter['n'] + counter['o'] + counter['p'] + counter['q'] + counter['r'] + counter['s'] + counter['t'] + counter['u']+ counter['v'] + counter['w'] + counter['x'] + counter['y'] + counter['z']
        LET = letterU + letterL

        counter['0'],counter['1'],counter['2'],counter['3'],counter['4'],counter['5'],counter['6'],counter['7'],counter['8'],counter['9']
        NUM = counter['0'] + counter['1'] + counter['2'] + counter['3'] + counter['4'] + counter['5'] + counter['6'] + counter['7'] + counter['8'] + counter['9']

        counter['\n'],counter['/t'],counter[' '],counter[':'],counter["'"],counter['"'],counter['<'],counter['>'],counter['='],counter['+'],counter['-'],counter['*'],counter['/']
        OTH = counter['\n'] + counter['/t'] + counter[' '] + counter[':'] + counter["'"] + counter['"'] + counter['<'] + counter['>'] + counter['='] + counter['+'] + counter['-'] + counter['*'] + counter['/']

        print 'Result Analysis \n'
        print 'Length of the String: ', len(string)
        print 'no. of Letters: ', LET
        print 'no. of Numbers: ', NUM
        print 'no. of Other Characters: ', OTH

        ans = raw_input('Try Again? [Y/N] ')

1 个答案:

答案 0 :(得分:0)

number = 0
alphabet = 0
special = 0
for i in string:
    if i.isnumeric():
        number +=1
    if i.isalpha():
        alphabet +=1
    if i.isascii():
        special +=1
print('no. of Number: ',number)
print('no. of letters: ',alphabet)
print('no. of Other Characters: ',special - (number+alphabet))`