在優雅的方法稍加改變由高性能馬克給出:
Select[Tuples[{a, b, c, d}, 3], OrderedQ]
排列的方法是更爲通用的(但不是你要找的是什麼?)
例如:
Select[Permutations[
[email protected]@ConstantArray[{a, b, c, d}, {3}], {2, 3}], OrderedQ]
給出以下內容
![alt text](https://i.stack.imgur.com/6AqM8.gif)
編輯:
Select[Tuples[[email protected]{a, b, d, c}, 3], OrderedQ]
可能是更好
編輯-2
當然,也可以使用案例。例如
Cases[Permutations[
[email protected]@ConstantArray[{a, b, d, c}, {3}], {2, 3}], _?OrderedQ]
編輯-3。
如果列表包含重複元素,兩種方法將有所不同。從 輸出下面(方法2)中,例如,將包含重複(其可以或可以不被期望的):
Select[Tuples[{a, b, c, d, a}, 3], OrderedQ]
它們可以容易地被排除:
[email protected][Tuples[{a, b, c, d, a}, 3], OrderedQ]
的以下的計算結果爲「真」(刪除從呈現接近2列表重複的元素,並顯示結果的方法1(高性能標記方法)中產生的列表:
lst = RandomInteger[9, 50];
Select[[email protected]@Tuples[lst, 3], OrderedQ] ==
[email protected]tes[Map[Sort, Tuples[lst, 3]]]
一樣噸他跟隨(從方法2的輸出中刪除重複,方法1的排序輸出):
lst = RandomInteger[9, 50];
[email protected][[email protected][lst, 3], OrderedQ] ==
[email protected][Map[Sort, Tuples[lst, 3]]]
對不起!
請注意`Sort [#]&`與'Sort'相同。 – dreeves 2010-12-01 15:53:30