Ade*_*mro 8 python language-agnostic algorithm shuffle
我喜欢在旅途中观看我最喜欢的电视节目.我有我在播放列表中关注的每个节目的所有剧集.并非所有节目都包含相同数量的剧集.与喜欢马拉松的人不同,我喜欢将一个节目的剧集与另一个节目的剧集交错.
例如,如果我有一个名为ABC的节目有2集,而一个名为XYZ的节目有4集,我希望我的播放列表看起来像:
XYZe1.mp4
ABCe1.mp4
XYZe2.mp4
XYZe3.mp4
ABCe2.mp4
XYZe4.mp4
Run Code Online (Sandbox Code Playgroud)
生成此交错播放列表的一种方法是将每个节目表示为剧集列表,并在所有节目上执行轻快的随机播放.人们可以编写一个函数来计算每一集在单位时间间隔内的位置(0.0到1.0独占,0.0是赛季开始,1.0是赛季结束),然后根据他们的位置对所有剧集进行排序.
我在Python 2.7中编写了以下简单函数来执行in-shuffle:
def riffle_shuffle(piles_list):
scored_pile = ((((item_position + 0.5) / len(pile), len(piles_list) - pile_position), item) for pile_position, pile in enumerate(piles_list) for item_position, item in enumerate(pile))
shuffled_pile = [item for score, item in sorted(scored_pile)]
return shuffled_pile
Run Code Online (Sandbox Code Playgroud)
要获取上述示例的播放列表,我只需要调用:
riffle_shuffle([['ABCe1.mp4', 'ABCe2.mp4'], ['XYZe1.mp4', 'XYZe2.mp4', 'XYZe3.mp4', 'XYZe4.mp4']])
Run Code Online (Sandbox Code Playgroud)
这在大多数情况下都能很好地工作.但是,有些情况下结果不是最佳的 - 播放列表中的两个相邻条目是来自同一节目的剧集.例如:
>>> riffle_shuffle([['ABCe1', 'ABCe2'], ['LMNe1', 'LMNe2', 'LMNe3'], ['XYZe1', 'XYZe2', 'XYZe3', 'XYZe4', 'XYZe5']])
['XYZe1', 'LMNe1', 'ABCe1', 'XYZe2', 'XYZe3', 'LMNe2', 'XYZe4', 'ABCe2', 'LMNe3', 'XYZe5']
Run Code Online (Sandbox Code Playgroud)
请注意,有两个并排出现的'XYZ'剧集.这种情况可以简单地修复(手动交换'ABCe1'和'XYZe2').
我很想知道是否有更好的方法在不同长度的多个列表上交错或执行riffle shuffle.我想知道您是否拥有更简单,更高效或简单优雅的解决方案.
belisarius提出的解决方案(谢谢!):
import itertools
def riffle_shuffle_belisarius(piles_list):
def grouper(n, iterable, fillvalue=None):
args = [iter(iterable)] * n
return itertools.izip_longest(fillvalue=fillvalue, *args)
if not piles_list:
return []
piles_list.sort(key=len, reverse=True)
width = len(piles_list[0])
pile_iters_list = [iter(pile) for pile in piles_list]
pile_sizes_list = [[pile_position] * len(pile) for pile_position, pile in enumerate(piles_list)]
grouped_rows = grouper(width, itertools.chain.from_iterable(pile_sizes_list))
grouped_columns = itertools.izip_longest(*grouped_rows)
shuffled_pile = [pile_iters_list[position].next() for position in itertools.chain.from_iterable(grouped_columns) if position is not None]
return shuffled_pile
Run Code Online (Sandbox Code Playgroud)
示例运行:
>>> riffle_shuffle_belisarius([['ABCe1', 'ABCe2'], ['LMNe1', 'LMNe2', 'LMNe3'], ['XYZe1', 'XYZe2', 'XYZe3', 'XYZe4', 'XYZe5']])
['XYZe1', 'LMNe1', 'XYZe2', 'LMNe2', 'XYZe3', 'LMNe3', 'XYZe4', 'ABCe1', 'XYZe5', 'ABCe2']
Run Code Online (Sandbox Code Playgroud)
确定性解决方案(即非随机)
通过减少剧集数量对节目进行排序.
选择最大的并排列一个矩阵,其列数对应于此剧集的剧集数,按以下方式填写:
A A A A A A <- First show consist of 6 episodes
B B B B C C <- Second and third show - 4 episodes each
C C D D <- Third show 2 episodes
Run Code Online (Sandbox Code Playgroud)
然后按列收集
{A,B,C}, {A,B,C}, {A,B,D}, {A,B,D}, {A,C}, {A,C}
Run Code Online (Sandbox Code Playgroud)
然后加入
{A,B,C,A,B,C,A,B,D,A,B,D,A,C,A,C}
Run Code Online (Sandbox Code Playgroud)
现在分配顺序号码
{A1, B1, C1, A2, B2, C2, A3, B3, D1, A4, B4, D2, A5, C3, A6, C4}
Run Code Online (Sandbox Code Playgroud)
编辑
你的情况
[['A'] * 2, ['L'] * 3, ['X'] * 5])
X X X X X
L L L A A
-> {X1, L1, X2, L2, X3, L3, X4, A1, X5, A2}
Run Code Online (Sandbox Code Playgroud)
编辑2
由于这里没有Python,也许Mathematica代码可能有一些用处:
l = {, , ,}; (* Prepare input *)
l[[1]] = {a, a, a, a, a, a};
l[[2]] = {b, b, b, b};
l[[3]] = {c, c, c, c};
l[[4]] = {d, d};
le = Length@First@l;
k = DeleteCases[ (*Make the matrix*)
Flatten@Transpose@Partition[Flatten[l], le, le, 1, {Null}], Null];
Table[r[i] = 1, {i, k}]; (*init counters*)
ReplaceAll[#, x_ :> x[r[x]++]] & /@ k (*assign numbers*)
->{a[1], b[1], c[1], a[2], b[2], c[2], a[3], b[3], d[1], a[4], b[4],
d[2], a[5], c[3], a[6], c[4]}
Run Code Online (Sandbox Code Playgroud)