Python 我能识别小写和大写字母,并把它们放在字典里。我需要将大小写值连接在一起

Python 我能识别小写和大写字母,并把它们放在字典里。我需要将大小写值连接在一起,python,dictionary,key,Python,Dictionary,Key,我需要知道一个字符显示多少次,并将结果保存到字典中。例如:{“e”:8,“s”:7}表示“e”显示8次,“s”显示7次。我必须使大小写值相同 我设法找出了每个角色的显示次数。我很难让大小写字母在一起而不是分开 counting_symbols = {} for letter in "Cryptography is the practice and study of techniques" \ " for secure communicat

我需要知道一个字符显示多少次,并将结果保存到字典中。例如:
{“e”:8,“s”:7}
表示“e”显示8次,“s”显示7次。我必须使大小写值相同

我设法找出了每个角色的显示次数。我很难让大小写字母在一起而不是分开

counting_symbols = {}
for letter in "Cryptography is the practice and study of techniques" \
              " for secure communication in the presence of third parties" \
              " called adversaries. More generally, cryptography is about" \
              " constructing and analyzing protocols that prevent third" \
              " parties or the public from reading private messages; various " \
              "aspects in information security such as data confidentiality, " \
              "data integrity, authentication, and non-repudiation are central " \
              "to modern cryptography. Modern cryptography exists at the" \
              " intersection of the disciplines of mathematics, computer science, " \
              "electrical engineering, communication science, and physics. Applications " \
              "of cryptography include electronic commerce, chip-based payment cards, " \
              "digital currencies, computer passwords, and military communications.":
    counting_symbols[letter] = counting_symbols.get(letter, 0) + 1



print(counting_symbols)

这使得大写字母和小写字母分开。有人能帮你把它们连在一起吗?

你计算它们的方法可以更具体一些。仅给出一个伪代码示例:

if(letter == ('S' or 's')):
  num_s = num_s+1

例如,您可以在函数中为每个字母复制并粘贴此行。最后,你可以返回这些数字。我希望你能理解我算法的要点。您可以通过多种方式实现它,我的算法非常具体。

您只需添加1行代码即可将字母转换为小写:

counting_symbols = {}
for letter in "Cryptography is the practice and study of techniques" \
          " for secure communication in the presence of third parties" \
          " called adversaries. More generally, cryptography is about" \
          " constructing and analyzing protocols that prevent third" \
          " parties or the public from reading private messages; various " \
          "aspects in information security such as data confidentiality, " \
          "data integrity, authentication, and non-repudiation are central " \
          "to modern cryptography. Modern cryptography exists at the" \
          " intersection of the disciplines of mathematics, computer science, " \
          "electrical engineering, communication science, and physics. Applications " \
          "of cryptography include electronic commerce, chip-based payment cards, " \
          "digital currencies, computer passwords, and military communications.":
letter = str.lower(letter)
counting_symbols[letter] = counting_symbols.get(letter, 0) + 1

print(counting_symbols)

您可以使用lower()函数降低所有字符,并将其保存到另一个变量中

使用collections库中的Counter类也是一个很好的实践。计数器对象自动计算一个或多个字符串中的字符数,并将数据保存在字典中

text = "Cryptography is the practice and study of techniques"
lower_chars = text.lower()

from collections import Counter
counter = Counter(lower_chars)
print(counter)

为什么不先将整个字符串小写或大写?没有测试,而是尝试
计数\u符号[letter.lower()]=计数\u符号.get(letter.lower(),0)+1
使用
letter=letter.lower()
letter=letter.upper()
在更新计数之前。因此这不是一个很好的答案,因为它需要复制和粘贴大量代码。处理全大写或全小写是更好的做法,因为它更简洁,更易于作为代码管理。一般来说,所有较低的是公约(特别是从问题来看)