Skip to main content
 首页 » 编程设计

python之如何获得字符串中每个字母的计数

2025年05月04日13java哥

我的任务是提供一个解密 Cesar 密码的程序,我正在查看之前在此站点上提出的其他问题并大致了解它。但是,我只是有一个非常基本的问题,关于如何计算字符串中的每个字母。

这是我到目前为止的想法:

Input=input("input the text you want  to decipher:") 
 
import string 
print(string.ascii_uppercase) 
 
def get_char(ch,shift): 
    #get a tally of the each letter 
    common_letter=#letter with most "tallies" 
    return common_letter 
    print(common_letter) 
 
#finding the shift 
def get_shift(s,ignore): 
    for x in Input: 
       shift=get_char-x 
       if shift=='e': 
           return x 
           print(x) 
 
def output_plaintext(s,shift): 
 
#convert back to English based off shift  
    pass  
 
def main(): 
# main body where i call together my other functions 
    pass  
 
input("is this decrypted?") 
#if no is inputted re run program with second most common letter 

如何计算字符串中每个字母的数量?

-弥敦道

请您参考如下方法:

这可能对您有帮助:-

from collections import Counter 
input='Nathannn' 
print Counter(input) 

输出:-

Counter({'n': 3, 'a': 2, 'h': 1, 't': 1, 'N': 1}) 

如果您想忽略大小写,请使用 input.lower() 然后应用 Counter(input)