弦分析

2024-09-28 03:18:15 发布

您现在位置: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] ')

Tags: ofnoinputstringrawcounternumprint
1条回答
网友
1楼 · 发布于 2024-09-28 03:18:15
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))`

相关问题 更多 >

    热门问题