英文:
code not executing trying to count how many times each letter appears in a piece of text
问题
I'm trying to count how many times each letter appears in a piece of text.
def count_letters(text):
result = {}
for letter in text:
if letter not in result:
result[letter] = 0
result[letter] += 1
return result
count_letters("aaad g a")
When I type this code, however, it's only showing 1 as the values for each key. I was expecting 4 for 'a', 1 for 'd', etc.
英文:
I'm trying to count how many times each letter appears in a piece of text.
def count_letters(text):
result = {}
for letter in text:
if letter not in result:
result[letter]= 0
result[letter] += 1
return result
count_letters("aaad g a")
When I type this code however, it's only showing 1 as the values for each key. I was expecting 4 for a, 1 for d, etc
答案1
得分: 2
以下是您要翻译的代码部分:
def count_letters(text):
result = {}
for letter in text:
if letter not in result:
result[letter] = 0
result[letter] += 1 # This must not happen only when you've not seen that letter yet, but each time
return result
print(count_letters("aaad g a"))
这段代码会输出:
{'a': 4, 'd': 1, ' ': 2, 'g': 1}
或者也许这个版本的代码更清晰?
def count_letters(text):
result = {}
for letter in text:
if letter not in result:
result[letter] = 1
else:
result[letter] += 1
return result
print(count_letters("aaad g a"))
这会在第一次看到该字母时将哈希值设为1,或者在以后的每一次增加计数。
英文:
Your indentation is off; you only increment the count the first time you see the letter.
def count_letters(text):
result = {}
for letter in text:
if letter not in result:
result[letter] = 0
result[letter] += 1 # This must not happen only when you've not seen that letter yet, but each time
return result
print(count_letters("aaad g a"))
This prints:
> {'a': 4, 'd': 1, ' ': 2, 'g': 1}
Or maybe this version of the code is clearer?
def count_letters(text):
result = {}
for letter in text:
if letter not in result:
result[letter] = 1
else:
result[letter] += 1
return result
print(count_letters("aaad g a"))
This sets the hash value to 1 the first time you see that letter, or increases the count every other time.
答案2
得分: 2
The result[letter] += 1
语句应该缩进以位于 if
语句下方,这样它只在第一次看到字母时执行。将缩进级别更改为直接位于 for
内部。
if letter not in result:
result[letter] = 0
result[letter] += 1
一个更简单的方法是使用 collections.defaultdict
,这可以避免在第一次出现时手动将值设置为 0
。
from collections import defaultdict
result = defaultdict(int)
for letter in text:
result[letter] += 1
或者更好的方法是,只需使用 collections.Counter
。
from collections import Counter
return Counter(text)
# 或者使用 {**Counter(text)} 来获得常规字典
英文:
The result[letter] += 1
statement is indented to be under the if
statement, so it will be only be executed the first time a letter is seen. Change the indentation level to be directly inside the for
instead.
if letter not in result:
result[letter] = 0
result[letter] += 1
A simpler method would be to use collections.defaultdict
, which obviates the need to manually set the value to 0
on the first occurrence.
from collections import defaultdict
result = defaultdict(int)
for letter in text:
result[letter] += 1
return result
Or even better, just use collections.Counter
.
from collections import Counter
return Counter(text)
# or {**Counter(text)} to get a regular dict
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论