【发布时间】:2015-07-03 08:12:49
【问题描述】:
我正在为办公室构建一个媒体播放器,到目前为止一切都很好,但我想添加一个投票系统(有点像 Pandora 竖起大拇指/竖起大拇指)
为了构建播放列表,我目前正在使用以下代码,它会随机抽取 100 个最近未播放过的曲目(我们确保所有曲目的播放次数大致相同),然后确保我们听不到10 首歌曲中的同一艺术家,并构建了一个包含 50 首歌曲的播放列表。
max_value = Items.select(fn.Max(Items.count_play)).scalar()
query = (Items
.select()
.where(Items.count_play < max_value, Items.count_skip_vote < 5)
.order_by(fn.Rand()).limit(100))
if query.count < 1:
max_value = max_value - 1
query = (Items
.select()
.where(Items.count_play < max_value, Items.count_skip_vote < 5)
.order_by(fn.Rand()).limit(100))
artistList = []
playList = []
for item in query:
if len(playList) is 50:
break
if item.artist not in artistList:
playList.append(item.path)
if len(artistList) < 10:
artistList.append(item.artist)
else:
artistList.pop(0)
artistList.append(item.artist)
for path in playList:
client.add(path.replace("/music/Library/",""))
我正在尝试找出使用赞成/反对票的最佳方式。 我希望看到更少的反对票和更多的赞成票。
我不追求直接代码,因为我对 python 很满意,更多的是我无法完全理解的逻辑(也就是说,如果你觉得需要改进我的代码,我会的不要阻止你:))
【问题讨论】:
标签: python random-sample