Skip to main content
 首页 » 编程设计

python返回两个字典

2025年05月04日16落叶无声

我正在尝试归还两本词典。 person_to_friends 和 person_to_networks 被赋予函数,profiles_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 

这只给我 person_to_friends 字典..任何人都可以解决这个问题吗?

我要返回的是

{person_to_friends}

{person_to_networks}

请您参考如下方法:

简单地做:

return (person_to_friends, person_to_networks) 

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

person_to_friends, person_to_networks = load_profiles(var1, var2, var3)