【发布时间】:2017-10-18 23:09:35
【问题描述】:
例子:
def test_get_word_len_dict():
text = "May your coffee be strong and your Monday be short"
the_dict = get_word_len_dict(text)
print_dict_in_key_order(the_dict)
print()
text = 'why does someone believe you when you say there are four billion stars but they have to check when you say the paint is wet'
the_dict = get_word_len_dict(text)
print_dict_in_key_order(the_dict)
我想得到一个字典,其中的键是 整数和对应的值,它们是唯一单词的列表。 与键对应的单词列表包含来自的所有唯一单词 长度等于键值的文本。相应的 唯一单词列表应按字母顺序排序。我不知道如何修复我的功能
def get_len_word_dict():
new_text = text.split()
for k,v in new_text.items():
if len(v) = len(k):
return k,v
预期:
2 : ['be']
3 : ['May', 'and']
4 : ['your']
5 : ['short']
6 : ['Monday', 'coffee', 'strong']
2 : ['is', 'to']
3 : ['are', 'but', 'say', 'the', 'wet', 'why', 'you']
4 : ['does', 'four', 'have', 'they', 'when']
5 : ['check', 'paint', 'stars', 'there']
7 : ['believe', 'billion', 'someone']
【问题讨论】:
-
一方面,您在 if 语句中有一个等号。你能解释一下你期望你的函数做什么,为什么?现在,即使您将
=更改为==,它也只会查找两个长度相同的单词并返回它们。 -
只需使用同一问题的先前答案之一,并在最后对 dict 中的列表进行排序。没有必要一直问同样的问题
标签: python