gpt4 book ai didi

python - 在字典中计算大写或非大写

转载 作者:行者123 更新时间:2023-11-28 16:57:13 30 4
gpt4 key购买 nike

所以我有这本字典,

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

而且我需要计算有多少是大写的和非大写的。我知道我需要通过获取字典的键并循环遍历它们来做到这一点,但我遇到了麻烦。请帮忙。谢谢!

尝试将计数器变量与 for 循环以及 isupper() 和 islower() 一起使用,但它不起作用。如果您有更好的方法,请告诉我!

#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

最佳答案

# 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

关于python - 在字典中计算大写或非大写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57237560/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com