Python 理解字符计数程序的代码

Python 理解字符计数程序的代码,python,dictionary,Python,Dictionary,我很难理解下面的代码是如何工作的。我使用了Python Visualiser,但这个过程对我来说似乎没有意义。我感到困惑的主要原因是for循环中的setdefault行和count[character]行 在您的示例中,对于dict计数中不存在的键,只需将每个字符的出现频率设置为0 count[character]是访问dict中key值的语法。在本例中,character是key message = 'This is a random sentence that forms a message

我很难理解下面的代码是如何工作的。我使用了Python Visualiser,但这个过程对我来说似乎没有意义。我感到困惑的主要原因是for循环中的setdefault行和count[character]行

在您的示例中,对于dict计数中不存在的键,只需将每个字符的出现频率设置为0

count[character]是访问dict中key值的语法。在本例中,character是key

message = 'This is a random sentence that forms a message' # text for which we want to find character frequency
count = {} # initiate the dictionary

for character in message.upper(): # Loop over every character in string coverted to upper case 
    count.setdefault(character, 0) # set the count of each character which is not yet present as a key in count dict to 0
    count[character] = count[character] + 1 # initiate the value by 1

print(count) 

从初始化变量开始:

message = 'This is a random sentence that forms a message'
count = {}
第一行很简单,第二行是一个空字典,您将在其中存储消息中某个字母出现的次数

在这里,您将开始循环消息中的所有字母,但首先所有字母都使用大写字母.upper

如果关键字符尚不在字典计数中,则将以默认值0插入。你可以阅读更多关于这方面的内容

此行将使字符的值增加1。这一行可以通过count[character]+=1来简化,而count[character]+=1的作用与此完全相同

关于这个问题,我已经解释了setdefault行为的用法,我不再重复了

我展示了三种不同的方法,包括您的方法,用于计算字符串中每个字符的出现次数,不区分大小写

from collections import Counter


def count_chars_1(message):
    count = {}
    for character in message.upper():  
        count.setdefault(character, 0)
        count[character] = count[character] + 1
    return count

def count_chars_2(message):
    count = {}
    for character in message.upper():  
        count[character] = count.get(character, 0)+1
    return count

def count_chars_3(message):
    return dict(Counter(message.upper()))

message = 'This is a random sentence that forms a message'
print(count_chars_1(message))
print(count_chars_2(message))
print(count_chars_3(message))
输出:

{'T': 4, 'H': 2, 'I': 2, 'S': 6, ' ': 8, 'A': 5, 'R': 2, 'N': 3, 'D': 1, 'O': 2, 'M': 3, 'E': 5, 'C': 1, 'F': 1, 'G': 1}
{'T': 4, 'H': 2, 'I': 2, 'S': 6, ' ': 8, 'A': 5, 'R': 2, 'N': 3, 'D': 1, 'O': 2, 'M': 3, 'E': 5, 'C': 1, 'F': 1, 'G': 1}
{'T': 4, 'H': 2, 'I': 2, 'S': 6, ' ': 8, 'A': 5, 'R': 2, 'N': 3, 'D': 1, 'O': 2, 'M': 3, 'E': 5, 'C': 1, 'F': 1, 'G': 1}
说明:

在count_chars_2方法中,我使用.get方法在count dictionary中搜索字符,如果未找到该字符,则将该字符的出现次数设置为0

在count_chars_3中,我使用collections包的计数器方法来计算发生的次数。count_chars_3返回一个dict对象以满足您的条件。可以阅读有关计数器的详细信息


关于setdefault,您不了解什么?关于count[character],您不了解什么?您读了吗?请不断提醒自己count是一个字典,而不是某个整数计数器。@mkrieger1对于setdefault,当出现重复字符时,我对它的功能感到困惑。在计数(字符)中,我错误地把它看作是一个整数计数器,正如罗纳德提到的。@ MKRIGER1谢谢,我没有考虑阅读文档。这为我消除了很多困惑:谢谢!!现在这就更有意义了。谢谢!count_chars_2和count_chars_3方法比我使用的方法直观得多。我非常感谢你的帮助。非常感谢,现在这样做更有意义了。
count.setdefault(character, 0)
count[character] = count[character] + 1
from collections import Counter


def count_chars_1(message):
    count = {}
    for character in message.upper():  
        count.setdefault(character, 0)
        count[character] = count[character] + 1
    return count

def count_chars_2(message):
    count = {}
    for character in message.upper():  
        count[character] = count.get(character, 0)+1
    return count

def count_chars_3(message):
    return dict(Counter(message.upper()))

message = 'This is a random sentence that forms a message'
print(count_chars_1(message))
print(count_chars_2(message))
print(count_chars_3(message))
{'T': 4, 'H': 2, 'I': 2, 'S': 6, ' ': 8, 'A': 5, 'R': 2, 'N': 3, 'D': 1, 'O': 2, 'M': 3, 'E': 5, 'C': 1, 'F': 1, 'G': 1}
{'T': 4, 'H': 2, 'I': 2, 'S': 6, ' ': 8, 'A': 5, 'R': 2, 'N': 3, 'D': 1, 'O': 2, 'M': 3, 'E': 5, 'C': 1, 'F': 1, 'G': 1}
{'T': 4, 'H': 2, 'I': 2, 'S': 6, ' ': 8, 'A': 5, 'R': 2, 'N': 3, 'D': 1, 'O': 2, 'M': 3, 'E': 5, 'C': 1, 'F': 1, 'G': 1}