ADVANCED = False
def count_unique(string1):
"""Count unique words in a string.
This function should take a single string and return a dictionary
that has all of the distinct words as keys, and the number of times
that word appears in the string.
For example:
>>> print_dict(count_unique("each word appears once"))
{'appears': 1, 'each': 1, 'once': 1, 'word': 1}
Words that appear more than once should be counted each time:
>>> print_dict(count_unique("rose is a rose is a rose"))
{'a': 2, 'is': 2, 'rose': 3}
It's fine to consider punctuation part of a word (e.g., a comma
at the end of a word can be counted as part of that word) and
to consider differently-capitalized words as different:
>>> print_dict(count_unique("Porcupine see, porcupine do."))
{'Porcupine': 1, 'do.': 1, 'porcupine': 1, 'see,': 1}
"""
unique_words = {}
words_list = string1.split()
for word in words_list:
if word not in unique_words:
unique_words[word] = 1
else:
unique_words[word] += 1
return unique_words
##############################################################################
# You can ignore everything after here
def print_dict(d):
# This method is just used to print dictionaries in key-alphabetical
# order, and is only used for our documentation tests. You can ignore it.
if isinstance(d, dict):
print "{" + ", ".join("%r: %r" % (k, d[k]) for k in sorted(d)) + "}"
else:
print d
def sort_pairs(l):
# Print sorted list of pairs where the pairs are sorted. This is used only
# for documentation tests. You can ignore it.
return sorted(sorted(pair) for pair in l)
if __name__ == "__main__":
print
import doctest
for k, v in globals().items():
if k[0].isalpha():
if k.startswith('adv_') and not ADVANCED:
continue
a = doctest.run_docstring_examples(v, globals(), name=k)
print "** END OF TEST OUTPUT"
我正在使用Python 2.7.6
ValueError: line 8 of the docstring for count_unique has inconsistent leading whitespace: ' Words that appear more than once should be counted each time:'
答案 0 :(得分:1)
与文档字符串一致:
def count_unique(string1):
"""Count unique words in a string.
This function should take a single string and return a dictionary
that has all of the distinct words as keys, and the number of times
that word appears in the string.
For example:
>>> print_dict(count_unique("each word appears once"))
{'appears': 1, 'each': 1, 'once': 1, 'word': 1}
Words that appear more than once should be counted each time:
>>> print_dict(count_unique("rose is a rose is a rose"))
{'a': 2, 'is': 2, 'rose': 3}
It's fine to consider punctuation part of a word (e.g., a comma
at the end of a word can be counted as part of that word) and
to consider differently-capitalized words as different:
>>> print_dict(count_unique("Porcupine see, porcupine do."))
{'Porcupine': 1, 'do.': 1, 'porcupine': 1, 'see,': 1}
"""
输出:
**********************************************************************
File "/home/padraic/test.py", line 30, in count_unique
Failed example:
print_dict(count_unique("each word appears once"))
Expected:
{'appears': 1, 'each': 1, 'once': 1, 'word': 1}
Words that appear more than once should be counted each time:
Got:
{'appears': 1, 'each': 1, 'once': 1, 'word': 1}
**********************************************************************
File "/home/padraic/test.py", line 33, in count_unique
Failed example:
print_dict(count_unique("rose is a rose is a rose"))
Expected:
{'a': 2, 'is': 2, 'rose': 3}
It's fine to consider punctuation part of a word (e.g., a comma
at the end of a word can be counted as part of that word) and
to consider differently-capitalized words as different:
Got:
{'a': 2, 'is': 2, 'rose': 3}
** END OF TEST OUTPUT
或缩进字符串:
def count_unique(string1):
"""Count unique words in a string.
This function should take a single string and return a dictionary
that has all of the distinct words as keys, and the number of times
that word appears in the string.
For example:
>>> print_dict(count_unique("each word appears once"))
{'appears': 1, 'each': 1, 'once': 1, 'word': 1}
Words that appear more than once should be counted each time:
>>> print_dict(count_unique("rose is a rose is a rose"))
{'a': 2, 'is': 2, 'rose': 3}
It's fine to consider punctuation part of a word (e.g., a comma
at the end of a word can be counted as part of that word) and
to consider differently-capitalized words as different:
>>> print_dict(count_unique("Porcupine see, porcupine do."))
{'Porcupine': 1, 'do.': 1, 'porcupine': 1, 'see,': 1}
答案 1 :(得分:0)
不确定,但也许你混合空格和制表符缩进?选择一个并坚持下去。如果使用选项卡,请将编辑器设置为4个空格宽度。如果选择空格,则每个缩进级别也应使用4个空格。