[英]I can identify lowercase and uppercase letters and put them in a dictionary. I need to join the uppercase and lowercase values together
I need to know how many times a character shows, and keep the results into a dictionary.我需要知道一个字符显示了多少次,并将结果保存到字典中。 For example: {"e": 8, "s": 7}
means "e" shows 8 times and "s" shows 7 times.例如: {"e": 8, "s": 7}
表示 "e" 显示 8 次, "s" 显示 7 次。 I have to make the upper and lower case values to be the same.我必须使大小写值相同。
I managed to be able to find out how many times each character is shown.我设法找出每个字符显示了多少次。 I am having trouble in getting the Uppercase and lowercase letters to be together and not separate.我无法将大写字母和小写字母放在一起而不是分开。
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)
That gives the Uppercase and lowercase letters separate.这将大写和小写字母分开。 Can someone help in in making them join together?有人可以帮助他们加入吗?
You just need to add 1 line of code to convert letter to lowercase:您只需添加 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)
You can lower all characters using lower() function and save it to another variable.您可以使用 lower() function 降低所有字符并将其保存到另一个变量。
It's also a good practice to use Counter class from collections library.使用 collections 库中的计数器 class 也是一个好习惯。 A Counter object automatically counts characters in a string or strings in a list and save the data in a dictionary.计数器 object 自动计算字符串中的字符或列表中的字符串,并将数据保存在字典中。
text = "Cryptography is the practice and study of techniques"
lower_chars = text.lower()
from collections import Counter
counter = Counter(lower_chars)
print(counter)
The way you count them can be more specific.您计算它们的方式可以更具体。 Just to give you a pseudo-code example:只是给你一个伪代码示例:
if(letter == ('S' or 's')):
num_s = num_s+1
You can just copy and paste this line for each letter, in a function for example.您可以为每个字母复制并粘贴这一行,例如在 function 中。 And at the end, you can return these numbers.最后,您可以返回这些数字。 I hope that you get the major point of my algorithm.我希望你能明白我算法的重点。 You can implement it in many ways, my algorithm is just to be very very specific.您可以通过多种方式实现它,我的算法只是非常具体。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.