I have a code which works pretty well if not returning keys with zero counts as values. I am still new to Python, so this may be a matter of a simple tweek, but don't know how So please how can I return 0 as a value of a key,if there are 0 counts.
dictionary = {}
for char in string:
if 'Z' >= char >= 'A':
dictionary.setdefault("upper", 0)
dictionary["upper"] = 1
elif 'z' >= char >= 'a':
dictionary.setdefault("lower", 0)
dictionary["lower"] = 1
elif char == ' ':
dictionary.setdefault("space", 0)
dictionary["space"] = 1
elif '9' >= char >= '0':
dictionary.setdefault("numeral", 0)
dictionary["numeral"] = 1
else:
dictionary.setdefault("punctuation", 0)
dictionary["punctuation"] = 1
return dictionary
print(count_types("aabbccc")) ```
Output is: {'lower': 7}
Desired output is: {'lower': 7, 'upper': 0, 'punctuation': 0, 'space': 0, 'numeral': 0}
CodePudding user response:
Try like this. Initialize the dictionary earlier
def count_types(string):
dictionary = {'lower':0, 'upper':0, 'space':0, 'numeral':0, 'punctuation':0}
for char in string:
if 'Z' >= char >= 'A':
dictionary.setdefault("upper", 0)
dictionary["upper"] = 1
elif 'z' >= char >= 'a':
dictionary.setdefault("lower", 0)
dictionary["lower"] = 1
elif char == ' ':
dictionary.setdefault("space", 0)
dictionary["space"] = 1
elif '9' >= char >= '0':
dictionary.setdefault("numeral", 0)
dictionary["numeral"] = 1
else:
dictionary.setdefault("punctuation", 0)
dictionary["punctuation"] = 1
return dictionary
print(count_types("aabbccc"))
output
{'lower': 7, 'upper': 0, 'space': 0, 'numeral': 0, 'punctuation': 0}
>
CodePudding user response:
The bug in your code is that if there are no numerical chars in the string for example, the code in the if statement never executes so the key will never get inserted to the dict.
What I suggest is preparing the dict in adavance with 0 as a default value, and then you can remove all of the defaultdict usage and make your code much shorter:
def count_chars(string):
dictionary = {'lower': 0, 'upper': 0, 'space': 0, 'numeral': 0, 'punctuation': 0}
for char in string:
if 'Z' >= char >= 'A':
dictionary["upper"] = 1
elif 'z' >= char >= 'a':
dictionary["lower"] = 1
elif char == ' ':
dictionary["space"] = 1
elif '9' >= char >= '0':
dictionary["numeral"] = 1
else:
dictionary["punctuation"] = 1
return dictionary
print(count_chars("testtest"))
CodePudding user response:
I see other answers But you can use import string
and use string.ascii_uppercase
or string.ascii_lowercase
or ... in if condition
like below:
import string
def count_types(chars):
dictionary = {'lower':0, 'upper':0, 'space':0, 'numeral':0, 'punctuation':0}
for char in chars:
if char in string.ascii_uppercase : dictionary["upper"] = 1
elif char in string.ascii_lowercase : dictionary["lower"] = 1
elif char == ' ' : dictionary["space"] = 1
elif char in string.digits : dictionary["numeral"] = 1
else : dictionary["punctuation"] = 1
return dictionary
print(count_types("a bBC123"))
Output:
{'lower': 2, 'upper': 2, 'space': 1, 'numeral': 3, 'punctuation': 0}