简体   繁体   中英

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. 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:

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.

It's also a good practice to use Counter class from collections library. A Counter object automatically counts characters in a string or strings in a list and save the data in a dictionary.

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM