Python 3.x 如何在字典中唯一地打印列表中的元素
我想知道如何在不打印副本的情况下唯一地打印出字典中每个列表的元素。对不起,我对编程很陌生。我正在阅读Python速成课程第二版,希望对我的代码增加一点挑战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
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中看到,您可以组合其中一些行,但为了清晰起见,我将每个步骤放在不同的行上