zyDE 9.15.1:嵌套字典示例:音乐库
zyDE 9.15.1: Nested dictionaries example: Music library
所以,我想为那些无法回答这个问题的人回答这个问题。
我想帮忙,所以这是我的答案,因为这对我来说有点困难。
如果你能想到更好的,告诉大家。
zyDE 9.15.1:嵌套词典示例:音乐库。
以下示例演示了一个使用 3 级嵌套字典创建简单音乐库的程序。
以下程序使用嵌套字典来存储一个小型音乐库。扩展程序,以便用户可以将艺术家、专辑和歌曲添加到库中。首先,添加一个将艺术家姓名添加到音乐词典的命令。然后添加添加专辑和歌曲的命令。在添加专辑之前注意检查字典中是否存在艺术家,在添加歌曲之前检查专辑是否存在。
答案:
music = {
'Pink Floyd': {
'The Dark Side of the Moon': {
'songs': [ 'Speak to Me', 'Breathe', 'On the Run', 'Money'],
'year': 1973,
'platinum': True
},
'The Wall': {
'songs': [ 'Another Brick in the Wall', 'Mother', 'Hey you'],
'year': 1979,
'platinum': True
}
},
'Justin Bieber': {
'My World':{
'songs': ['One Time', 'Bigger', 'Love Me'],
'year': 2010,
'platinum': True
}
}
}
prompt = ("1. Enter artist information\n"
"2. Exit\n")
command = ''
while command != '2':
command = input(prompt).lower()
if command == '1':
artist = input('Artist: ')
if artist in music.keys():
print('That artist already exists. Please try again.')
artist = input('Artist: ')
album = input('Album: ')
for albums in music.values():
if album in albums:
print('That album already exists. Please try again')
album = input('Album: ')
songs = input('Song: ').split()
music[artist] = {album: {'songs': songs}}
else:
break
print(music)
也许你可以这样试试:
prompt = ("1. Enter artist information\n"
"2. Exit\n")
command = ''
while command != '2':
command = input(prompt).lower()
if command == '1':
artist = input('Artist: ')
album = input('Album: ')
songs = input('Song: ').split()
if artist in music.keys():
print(music[artist].keys())
if album in music[artist].keys():
music[artist][album]["songs"] += songs
else:
music[artist].update({album: {}})
music[artist][album].update({"songs": songs})
else:
music.update({artist: {}})
music[artist].update({album: {}})
music[artist][album].update({"songs": songs})
print('\n', music)
所以,我想为那些无法回答这个问题的人回答这个问题。 我想帮忙,所以这是我的答案,因为这对我来说有点困难。 如果你能想到更好的,告诉大家。
zyDE 9.15.1:嵌套词典示例:音乐库。
以下示例演示了一个使用 3 级嵌套字典创建简单音乐库的程序。
以下程序使用嵌套字典来存储一个小型音乐库。扩展程序,以便用户可以将艺术家、专辑和歌曲添加到库中。首先,添加一个将艺术家姓名添加到音乐词典的命令。然后添加添加专辑和歌曲的命令。在添加专辑之前注意检查字典中是否存在艺术家,在添加歌曲之前检查专辑是否存在。
答案:
music = {
'Pink Floyd': {
'The Dark Side of the Moon': {
'songs': [ 'Speak to Me', 'Breathe', 'On the Run', 'Money'],
'year': 1973,
'platinum': True
},
'The Wall': {
'songs': [ 'Another Brick in the Wall', 'Mother', 'Hey you'],
'year': 1979,
'platinum': True
}
},
'Justin Bieber': {
'My World':{
'songs': ['One Time', 'Bigger', 'Love Me'],
'year': 2010,
'platinum': True
}
}
}
prompt = ("1. Enter artist information\n"
"2. Exit\n")
command = ''
while command != '2':
command = input(prompt).lower()
if command == '1':
artist = input('Artist: ')
if artist in music.keys():
print('That artist already exists. Please try again.')
artist = input('Artist: ')
album = input('Album: ')
for albums in music.values():
if album in albums:
print('That album already exists. Please try again')
album = input('Album: ')
songs = input('Song: ').split()
music[artist] = {album: {'songs': songs}}
else:
break
print(music)
也许你可以这样试试:
prompt = ("1. Enter artist information\n"
"2. Exit\n")
command = ''
while command != '2':
command = input(prompt).lower()
if command == '1':
artist = input('Artist: ')
album = input('Album: ')
songs = input('Song: ').split()
if artist in music.keys():
print(music[artist].keys())
if album in music[artist].keys():
music[artist][album]["songs"] += songs
else:
music[artist].update({album: {}})
music[artist][album].update({"songs": songs})
else:
music.update({artist: {}})
music[artist].update({album: {}})
music[artist][album].update({"songs": songs})
print('\n', music)