python返回两个字典

use*_*470 1 python dictionary function

我试图退还两本字典。给person_to_friends和person_to_networks函数,而profile_file是文本文件。我写的是:

def load_profiles(profiles_file, person_to_friends, person_to_networks):
    """
    (file, dict of {str : list of strs}, dict of {str : list of strs}) -> NoneType
    Update person to friends and person to networks dictionaries to include
    the data in open file.
    """
    profiles_file = open('data.txt', 'r')
    person_to_friends = person_to_friends(profiles_file)
    person_to_networks = person_to_networks(profiles_file)    
    return person_to_friends, person_to_networks
Run Code Online (Sandbox Code Playgroud)

这只给了我person_to_friends字典。.有人可以帮助解决这个问题吗?我要退货的是

{person_to_friends} {person_to_networks}

dur*_*rsk 5

只需做:

return (person_to_friends, person_to_networks)
Run Code Online (Sandbox Code Playgroud)

当调用该函数时,您需要解压缩返回值:

person_to_friends, person_to_networks = load_profiles(var1, var2, var3)
Run Code Online (Sandbox Code Playgroud)

  • “ return(a,b)”与“ return a,b`”相同,它是创建元组的逗号,而不是括号。 (4认同)