Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Counting capitalized or non-capitalize in dictionary

Tags:

python

So I have this dictionary,

word_counts = {'Two':2, 'one':3, 'Forks.':1, 'knife.':2, 
'glasses.':2, 'plate.':1, 'Naptkin.':1, 'his':2}

And i need to compute how to how many are capitalized and non-capitalized. I know I need to do this by getting the keys of the dictionary and loop through them all, but I'm having trouble. Please help. Thank you!

Tried to use counter variables with for loops and isupper() and islower() but its not working. If you haver better ways please let me know!

#What ive done so far
word_counts = {'Two':2, 'one':3, 'Forks.':1, 'knife.':2, 
    'glasses.':2, 'plate.':1, 'Naptkin.':1, 'his':2}

for word, occurence in words:
        upper_counter = 0
        lower_counter = 0
        for word in word_counts.items():
            if word.isupper():
                upper_counter += 1
            elif word_islower():
                lower_counter += 1
like image 824
Leonardo Echeverria Avatar asked Dec 08 '25 09:12

Leonardo Echeverria


1 Answers

# The counters should be initialised before the loop
upper_counter = 0
lower_counter = 0

for word in word_counts:  # Looping over a dictionary gives the keys
    if word[0].isupper():  # Just have to check if the first character is upper case
        upper_counter += 1
    else:
        lower_counter += 1
like image 113
Iain Shelvington Avatar answered Dec 10 '25 22:12

Iain Shelvington



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!