Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 如何在字典中唯一地打印列表中的元素_Python 3.x_Dictionary_Nested_Nested Lists - Fatal编程技术网

Python 3.x 如何在字典中唯一地打印列表中的元素

Python 3.x 如何在字典中唯一地打印列表中的元素,python-3.x,dictionary,nested,nested-lists,Python 3.x,Dictionary,Nested,Nested Lists,我想知道如何在不打印副本的情况下唯一地打印出字典中每个列表的元素。对不起,我对编程很陌生。我正在阅读Python速成课程第二版,希望对我的代码增加一点挑战 favorite_languages = { 'jen': ['python', 'c'], 'mark': ['c'], 'tom': ['ruby', 'haskell'], 'amy': ['python', 'ruby'], } print("Mentioned languages:&quo

我想知道如何在不打印副本的情况下唯一地打印出字典中每个列表的元素。对不起,我对编程很陌生。我正在阅读Python速成课程第二版,希望对我的代码增加一点挑战

favorite_languages = {
    'jen': ['python', 'c'],
    'mark': ['c'],
    'tom': ['ruby', 'haskell'],
    'amy': ['python', 'ruby'],
}

print("Mentioned languages:")

for languages in favorite_languages.values(): # Loop over the dictionaries unique values only
    if len(languages) !=  1:
        languages = set(tuple(languages))
        for language in languages:
            print("\t",language.title())


您可以通过将这些列表添加到空白列表中,并在末尾使用集合,将它们组合为常用的_语言.values()

print("Mentioned languages:")

languages = []
for language in favorite_languages.values():
    languages += language

languages = set(languages)

for language in languages:
    print("\t",language.title())
您将在PCC中看到,您可以组合其中一些行,但为了清晰起见,我将每个步骤放在不同的行上