【发布时间】:2015-12-04 01:33:14
【问题描述】:
https://gyazo.com/672475d1961538af601bcfa2781f3ef2 如您所见,“得到”和“预期”列表是相同的,但由于其对象的顺序是随机的,因此它给了我一个 doctest 失败。我的代码无法更改,因此这种随机化是不可避免的。我如何解决这个 doctest 问题。这是不起作用的功能。
def all_followers(data_dict, followed_user):
""" {str: dict of {str: object}}, str -> list of str
Returns a list containing the username of all the users in data_dict
that are following followed_user
>>> all_followers(process_data(open("small_data.txt")), "katieH")
['tomCruise']
>>> all_followers(process_data(open("rdata.txt")), "arrington")
['AccordionGuy', 'vkhosla', 'bhorowitz', 'peterfenton', 'mattcohler', 'michaelcvet', 'google', 'KatieS']
"""
# The list to be returned, is created
followers_list = []
for key in data_dict:
# Every username in data_dict and their "following" list is checked to
# see if it matches the username of the followed_user
if followed_user in data_dict[key]["following"]:
# If key follows followed_user, the name of key is appended to the
# followers_list
followers_list.append(key)
return followers_list
【问题讨论】:
-
请避免使用链接和图片(尤其是图片链接),而是将代码(以及由此产生的错误消息)直接粘贴到您的问题中。