我可以识别小写和大写字母并将它们放入字典中。我需要将大写和小写值连接在一起
I can identify lowercase and uppercase letters and put them in a dictionary. I need to join the uppercase and lowercase values together
我需要知道一个字符出现了多少次,并将结果存入字典。例如:{"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
您可以为每个字母复制并粘贴这一行,例如在一个函数中。最后,您可以 return 这些数字。我希望你能理解我的算法的要点。您可以通过多种方式实现它,我的算法只是非常非常具体。
您只需添加 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 库中的计数器 class 也是一个好习惯。计数器 object 自动对字符串中的字符或列表中的字符串进行计数,并将数据保存在字典中。
text = "Cryptography is the practice and study of techniques"
lower_chars = text.lower()
from collections import Counter
counter = Counter(lower_chars)
print(counter)
我需要知道一个字符出现了多少次,并将结果存入字典。例如:{"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
您可以为每个字母复制并粘贴这一行,例如在一个函数中。最后,您可以 return 这些数字。我希望你能理解我的算法的要点。您可以通过多种方式实现它,我的算法只是非常非常具体。
您只需添加 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 库中的计数器 class 也是一个好习惯。计数器 object 自动对字符串中的字符或列表中的字符串进行计数,并将数据保存在字典中。
text = "Cryptography is the practice and study of techniques"
lower_chars = text.lower()
from collections import Counter
counter = Counter(lower_chars)
print(counter)