即使输出正确,我的doctest也给了我失败的例子

时间:2015-12-04 01:33:15

标签: python doctest

https://gyazo.com/672475d1961538af601bcfa2781f3ef2 正如您所看到的,“Got”和“Expected”列表是相同的,但随着其对象的顺序随机化,它给我一个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

1 个答案:

答案 0 :(得分:0)

如果您不需要测试订单,请使用sorted

换行