我正在试图找出如何将两个对象“合并”在一起。我的目标是合并两个Album
对象(下面的代码)。只有当tracks
对象的'title'参数相同时,我才能合并多个Album
对象的Album
参数(列表)。
基本上,如果我有一个Album
对象,其中tracks
参数的长度为1,另一个相册对象的tracks
参数的长度也是1,则新的,或更新的单数Album
对象需要一个长度为2的tracks
参数。
我发布了我的代码,以显示如何定义对象。
提前致谢!
编辑:由于tracks
参数列表中的每个元素都是歌曲的名称,我希望保留相同的元素并将它们放入新的或更新的tracks
参数中。我需要将每个对象的确切元素放入这个“新”对象中,而不仅仅是改变元素的数量。
class Album(object) :
def __init__(self, artist, title, tracks = None) :
tracks = []
self.artist = artist
self.title = title
self.tracks = tracks
def add_track(self, track) :
self.track = track
(self.tracks).append(track)
print "The track %s was added." % (track)
def __str__(self) :
if len(self.tracks) == 1 :
return "Artist: %s, Album: %s [" % (self.artist, self.title) + "1 Track]"
return "Artist: %s, Album: %s [" % (self.artist, self.title) + str(len(self.tracks)) + " Tracks]"
答案 0 :(得分:2)
合并算法必须知道类的内部数据结构。因此,将合并代码放在类中似乎是合乎逻辑的。下面的代码执行此操作,并允许合并两个相册,只需添加它们(album1 + album2
):
class Album(object) :
def __init__(self, artist, title, tracks = None) :
self.artist = artist
self.title = title
self.tracks = tracks
def add_track(self, track) :
self.track = track
(self.tracks).append(track)
print "The track %s was added." % (track)
def __str__(self) :
if len(self.tracks) == 1 :
return "Artist: %s, Album: %s [" % (self.artist, self.title) + "1 Track]"
return "Artist: %s, Album: %s [" % (self.artist, self.title) + str(len(self.tracks)) + " Tracks]"
def __add__(self, other):
if self.artist != other.artist or self.title != other.title:
raise ValueError("Albums are incommensurable")
return Album(self.artist, self.title, self.tracks + other.tracks)
使用如下:
>>> a = Album('Joe', "Joe's First", tracks=['Beer', 'Trucks'])
>>> b = Album('Joe', "Joe's First", tracks=['Bourbon', 'Tequila'])
>>> complete = a + b
>>> print complete
Artist: Joe, Album: Joe's First [4 Tracks]
>>> complete.tracks
['Beer', 'Trucks', 'Bourbon', 'Tequila']
答案 1 :(得分:0)
关于保留相同的元素,您不需要在添加曲目之前只需要if
条件进行检查:
def add_track(self, track) :
self.track = track
if track not in (self.tracks) :
(self.tracks).append(track)
else :
raise ValueError("duplicate track")
print "The track %s was added." % (track)
答案 2 :(得分:0)
def merge_albums(album1, album2):
if not album1.artist == album2.artist and \
album1.title == album2.title:
raise SomeException("Albums don't match")
new_tracks = list(set(album1.tracks + album2.tracks))
return Album(album1.artist, album1.title, new_tracks)
答案 3 :(得分:0)
我假设只有当艺术家和标题相同时才会合并专辑的曲目。一种方法是通过定义您自己的合并函数,如下所示:
class Album(object) :
def __init__(self, artist, title, tracks = None) :
tracks = []
self.artist = artist
self.title = title
self.tracks = tracks
def add_track(self, track) :
self.track = track
(self.tracks).append(track)
print "The track %s was added." % (track)
def merge(self, album):
if (type(self)==type(album)):
if self.artist == album.artist and self.title==album.title:
self.tracks.extend(album.tracks)
else:
print "cannot merge albums, artists or titles are not same"
else:
print "Invalid object types, cannot merge"
def __str__(self) :
if len(self.tracks) == 1 :
return "Artist: %s, Album: %s [" % (self.artist, self.title) + "1 Track]"
return "Artist: %s, Album: %s [" % (self.artist, self.title) + \
str(len(self.tracks)) + " Tracks]"
然后,如果你有专辑a,你可以调用a.merge(b)合并后a.tracks应该包含合并的曲目。您需要根据需要进行其他更改。