我正在尝试进行以下练习: 考虑一下句子“吉姆很快意识到漂亮的礼服很贵”。创建一个字典count_letters,其键由句子中每个唯一字母组成,值由该单词中每个字母使用次数组成。在字典中分别计算大写和小写字母。
下面是我的代码,我认为它正在执行练习所要求的内容,但是由于某种原因,它仍然表示我没有正确执行操作。有任何想法吗?
sentence = 'Jim quickly realized that the beautiful gowns are expensive'
count_letters = {}
cnt_lowercase = 0
cnt_uppercase = 0
#write your code here!
for c in sentence:
if c.islower():
if (c in count_letters) == False:
count_letters[c]={c:sentence.count(c)}
cnt_lowercase += 1
if c.isupper():
if (c in count_letters) == False:
count_letters[c]={c:sentence.count(c)}
cnt_uppercase += 1
print(str(cnt_lowercase))
print(str(cnt_uppercase))
print(count_letters)
答案 0 :(得分:1)
from collections import Counter
count_letters = Counter('Jim quickly realized that the beautiful gowns are expensive')
# this gives a dictionary of character -> count
# if you need to skip spaces/punctuations (you probably do), use this
count_letters = Counter(c for c in 'Jim quickly realized that the beautiful gowns are expensive' if c.isalpha())
答案 1 :(得分:1)
您可以使用称为list comprehensions的功能来实现
alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
sentence = 'Jim quickly realized that the beautiful gowns are expensive'
counts = dict([[letter, sentence.count(letter)] for letter in set(sentence) if letter in alphabet])
答案 2 :(得分:0)
问题是count_letters
是一本字典,键是字母,值是带有字母计数的字典,在练习中,您被要求计算
值,由每个字母在此使用的次数组成 句子。
您的代码:
sentence = 'Jim quickly realized that the beautiful gowns are expensive'
count_letters = {}
cnt_lowercase = 0
cnt_uppercase = 0
for c in sentence:
if c.islower():
if (c in count_letters) == False:
count_letters[c]={c:sentence.count(c)}
cnt_lowercase += 1
if c.isupper():
if (c in count_letters) == False:
count_letters[c]={c:sentence.count(c)}
cnt_uppercase += 1
print(count_letters)
输出
{'u': {'u': 3}, 'g': {'g': 1}, 'q': {'q': 1}, 'l': {'l': 3}, 'o': {'o': 1}, 'm': {'m': 1}, 'f': {'f': 1}, 'k': {'k': 1}, 'z': {'z': 1}, 'w': {'w': 1}, 'a': {'a': 4}, 'n': {'n': 2}, 'c': {'c': 1}, 'y': {'y': 1}, 'r': {'r': 2}, 'b': {'b': 1}, 'h': {'h': 2}, 'd': {'d': 1}, 'e': {'e': 8}, 'i': {'i': 5}, 'v': {'v': 1}, 'p': {'p': 1}, 's': {'s': 2}, 'x': {'x': 1}, 'J': {'J': 1}, 't': {'t': 4}}
例如'u'
的值为{'u': 3}
。您可以添加以下行对其进行修复:
count_letters = { k : v[k] for k, v in count_letters.items()}
print(count_letters)
输出 (带有新行)
{'m': 1, 'c': 1, 'f': 1, 'b': 1, 'q': 1, 'd': 1, 'o': 1, 'g': 1, 'k': 1, 'r': 2, 'z': 1, 'v': 1, 'u': 3, 'l': 3, 'y': 1, 'p': 1, 's': 2, 'e': 8, 'x': 1, 'i': 5, 'w': 1, 'h': 2, 'n': 2, 'J': 1, 'a': 4, 't': 4}
说明
添加的行被称为字典理解。等效于:
d = {}
for k, v in count_letters.items():
d[k] = v[k]
count_letters = d
答案 3 :(得分:0)
您的词典中嵌套了其他字典,情况并非如此
例如
{'u': {'u': 3}}
应该只是
{'u': 3}
我简化了您的代码,它现在可以正常工作:
sentence = 'Jim quickly realized that the beautiful gowns are expensive'
count_letters = {}
cnt_lowercase = 0
cnt_uppercase = 0
for c in sentence:
if (c in count_letters):
count_letters[c] += 1
else:
count_letters[c] = 1
cnt_lowercase = len([i for i in count_letters.keys() if i.islower()])
cnt_uppercase = len([i for i in count_letters.keys() if i.isupper()])
print(count_letters)
print(cnt_lowercase)
print(cnt_uppercase)
答案 4 :(得分:0)
您应该没有字典的字典,而应该有一个直接返回字符计数的字典,这意味着例如,当您输入count_letters['J']
时,您得到 1 作为输出。
在您的代码中,当我输入count_letters['J']
时,我得到{'J': 1}
作为输出。
这是我编写此代码的方式:
sentence = 'Jim quickly realized that the beautiful gowns are expensive'
count_letters = {}
cnt_lowercase = 0
cnt_uppercase = 0
# iterate over all chars in the sentence string
for char in sentence:
# check if the char is lowercase
if char.islower():
cnt_lowercase += 1
# check if the char is upper case
elif char.isupper():
cnt_uppercase += 1
# count occurrence of each char in sentence string
b = count_letters.get(char, 0)
count_letters[char] = b + 1
# check the output
print(count_letters)
您将得到以下输出:
{'J': 1, 'i': 5, 'm': 1, ' ': 8, 'q': 1, 'u': 3, 'c': 1, 'k': 1, 'l': 3, 'y': 1, 'r': 2, 'e': 8, 'a': 4, 'z': 1, 'd': 1, 't': 4, 'h': 2, 'b': 1, 'f': 1, 'g': 1, 'o': 1, 'w': 1, 'n': 2, 's': 2, 'x': 1, 'p': 1, 'v': 1}
答案 5 :(得分:0)
这是我对列表理解的建议
注意:请在数字中分别计算大写和小写字母 字典。
sentence = 'Jim quickly realized that the beautiful gowns are expensive'
alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
count_letters_upper = ([[letter, sentence.count(letter)] for letter in set(sentence) if letter in alphabet if letter.isupper()])
count_letters_lower = ([[letter, sentence.count(letter)] for letter in set(sentence) if letter in alphabet if letter.islower()])
答案 6 :(得分:0)
这是上述问题的解决方案。您可以根据需要进行修改。
alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
sentence = 'Jim quickly realized that the beautiful gowns are expensive'
count_letters = dict()
for letter in sentence:
if letter in alphabet:
if letter in count_letters.keys():
count_letters[letter] += 1
else:
count_letters[letter] = 1
print(list(count_letters.values())[2])
print(list(count_letters.values())[7])
答案 7 :(得分:0)
sentence = 'Jim quickly realized that the beautiful gowns are expensive'
count_upper = {}
count_lower = {}
count_letters = {}
# write your code here!
for c in sentence:
if c.isupper():
if (c in count_upper):
count_upper[c] +=1
else:
count_upper[c] =1
if c.islower():
if (c in count_lower):
count_lower[c] +=1
else:
count_lower[c] =1
count_letters = {**count_upper, **count_lower} ;
print(count_letters)
答案 8 :(得分:-1)
将缓存的建立和计数逻辑委托给单独的功能:
def build_cache(sentence):
char_to_count_cache = {}
for ch in sentence:
if ch.isalnum():
char_to_count_cache[ch] = char_to_count_cache.get(ch, 0) + 1
return char_to_count_cache
def get_upper_and_lower_counts(char_to_count_cache):
num_lower_letters, num_upper_letters = 0, 0
for k in char_to_count_cache:
if k.islower():
num_lower_letters += 1
else:
num_upper_letters += 1
return num_lower_letters, num_upper_letters
驱动程序:
sentence = 'Jim quickly realized that the beautiful gowns are expensive'
char_to_count_cache = build_cache(sentence)
num_lower_letters, num_upper_letters = get_upper_and_lower_counts(char_to_count_cache)
total_letters = len(char_to_count_cache)
print total_letters
print num_lower_letters
print num_upper_letters
输出:
26
25
1