首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我能识别小写字母和大写字母,并把它们放在字典中。我需要将大写和小写值连接在一起。

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

Stack Overflow用户
提问于 2020-12-28 18:44:58
回答 3查看 137关注 0票数 0

我需要知道一个字符显示了多少次,并将结果保存在字典中。例如:{"e": 8, "s": 7}的意思是"e“显示8次,"s”显示7次。我必须使上下大写的值是相同的。

我设法找出每个字符显示了多少次。我很难把大写字母和小写字母放在一起,而不是分开。

代码语言:javascript
复制
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)

这使大写字母和小写字母分开。有人能帮忙让他们联合起来吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2020-12-28 18:58:24

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

代码语言:javascript
复制
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)
票数 0
EN

Stack Overflow用户

发布于 2020-12-28 19:03:50

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

使用集合库中的计数器类也是一个很好的实践。计数器对象会自动计数字符串中的字符或列表中的字符串,并将数据保存在字典中。

代码语言:javascript
复制
text = "Cryptography is the practice and study of techniques"
lower_chars = text.lower()

from collections import Counter
counter = Counter(lower_chars)
print(counter)
票数 0
EN

Stack Overflow用户

发布于 2020-12-28 18:56:29

你计算它们的方式可以更具体。为了给您提供一个伪代码示例:

代码语言:javascript
复制
if(letter == ('S' or 's')):
  num_s = num_s+1

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

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65482502

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档