以前贴吧讨论过这个问题,你找的函数应该是python中的itertools.combinations_with_replacement吧,现在那个帖子找不到了。下面是一种方法,速度一般
combinWithRep[list_List, k_Integer] := Block[{v, it, cf},
v = Table[Unique[], {k}];
it = Thread[{v, {1}~Join~Most@v, Length@list}];
cf = {it, First /@ it} /. {{iter__}, var_} :>
Hold@Block[{bag = Internal`Bag@Rest@{0}},
Do[Internal`StuffBag[bag, var, 1], iter];
bag~Internal`BagPart~All] /. Hold[x_] :> Compile[{}, x];
Part[list, cf[]]~Partition~k
];
combinWithRep[list_List, k_Integer] := Block[{v, it, cf},
v = Table[Unique[], {k}];
it = Thread[{v, {1}~Join~Most@v, Length@list}];
cf = {it, First /@ it} /. {{iter__}, var_} :>
Hold@Block[{bag = Internal`Bag@Rest@{0}},
Do[Internal`StuffBag[bag, var, 1], iter];
bag~Internal`BagPart~All] /. Hold[x_] :> Compile[{}, x];
Part[list, cf[]]~Partition~k
];