简体   繁体   中英

for every letter in the alphabet list Create a variable to store the frequency of each letter in the string and assign it an initial value of zero

Create a variable to store the given string "You can have data without information, but you cannot have information without data." Convert the given string to lowercase Create a list containing every lowercase letter of the English alphabet

for every letter in the alphabet list: Create a variable to store the frequency of each letter in the string and assign it an initial value of zero for every letter in the given string: if the letter in the string is the same as the letter in the alphabet list increase the value of the frequency variable by one. if the value of the frequency variable does not equal zero: print the letter in the alphabet list followed by a colon and the value of the frequency variable

myvar = "You can have data without information, but you cannot have information without data." print(myvar.lower()) letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

I'm stuck on "for every letter in the alphabet list: Create a variable to store the frequency of each letter in the string and assign it an initial value of zero"

You could use a dictionary:

import string

{i: 0 for i in string.ascii_lowercase}

Output:

{'a': 0, 'b': 0, 'c': 0, 'd': 0, 'e': 0, 'f': 0, 'g': 0, 'h': 0, 'i': 0, 'j': 0, 'k': 0, 'l': 0, 'm': 0, 'n': 0, 'o': 0, 'p': 0, 'q': 0, 'r': 0, 's': 0, 't': 0, 'u': 0, 'v': 0, 'w': 0, 'x': 0, 'y': 0, 'z': 0}

Edit:

You could use a Counter to solve this.

Try this to get the count of each character in myvar;

import string

dct = {i: 0 for i in string.ascii_lowercase}

myvar = "You can have data without information, but you cannot have information without data."

for i in myvar.lower():
    if i in dct.keys():
        dct[i] = dct[i] + 1

Hope this Helps...

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