Javascript中的Itertools.compositions

Javascript中的Itertools.compositions,javascript,Javascript,JavaScript中是否有类似于Python的itertools的库?我对排列和组合特别感兴趣 我没有使用Node.js 我想这样做: array = ['a', 'b', 'c', 'd']; //return non-duplicate combinations of length 2 ['a', 'b'] ['a', 'c'] ['a', 'd'] ['b', 'c'] ['b', 'd'] ['c', 'd'] 谢谢!:) 对于笛卡尔产品,您可能需要检查利用lodash的详细示例。

JavaScript中是否有类似于Python的itertools的库?我对排列和组合特别感兴趣

我没有使用Node.js

我想这样做:

array = ['a', 'b', 'c', 'd'];

//return non-duplicate combinations of length 2
['a', 'b']
['a', 'c']
['a', 'd']
['b', 'c']
['b', 'd']
['c', 'd']

谢谢!:)

对于笛卡尔产品,您可能需要检查利用
lodash
的详细示例。该功能与Python中的类似。

您可以使用递归方法来指定指定大小的给定数组的排列

函数getPermutations(数组、大小){ 函数p(t,i){ 如果(t.length==大小){ 结果:推(t); 返回; } if(i+1>array.length){ 返回; } p(t.concat(数组[i]),i+1); p(t,i+1); } var结果=[]; p([],0); 返回结果; } 变量数组=['a','b','c','d']; log(getPermutations(数组,2))
。作为控制台包装器{max height:100%!important;top:0;}
您可以使用my es iter库,它几乎是Python itertools的一对一端口,但采用JS方式


我喜欢
itertools.compositions
,想要一个内存效率高的JavaScript,但无法很快找到一个可接受的库,所以我推出了自己的库

它在TypeScript中(帮助我跟踪记账),但我将在底部附加传输的JavaScript

function* range(start: number, end: number) {
  for (; start <= end; ++start) { yield start; }
}

function last<T>(arr: T[]) { return arr[arr.length - 1]; }

function* numericCombinations(n: number, r: number, loc: number[] = []): IterableIterator<number[]> {
  const idx = loc.length;
  if (idx === r) {
    yield loc;
    return;
  }
  for (let next of range(idx ? last(loc) + 1 : 0, n - r + idx)) { yield* numericCombinations(n, r, loc.concat(next)); }
}

function* combinations<T>(arr: T[], r: number) {
  for (let idxs of numericCombinations(arr.length, r)) { yield idxs.map(i => arr[i]); }
}
Node在不到2.5分钟的时间内打印出
133784560
,并且在我的2015款老式笔记本电脑上以最小的内存使用量打印出来。也就是说,它生成了所有一亿三千四百万种方式,您可以从一副标准的五十二张扑克牌中选择七张扑克牌(即,所有完整的德州扑克),而不会增加内存负担或过度嵌套递归函数调用

(Python3可以在20秒内完成这项工作,或比之前快7倍……加速到上述欢迎的速度。)


JavaScript代码:

function* range(start, end) {
  for (; start <= end; ++start) { yield start; }
}
function last(arr) { return arr[arr.length - 1]; }
function* numericCombinations(n, r, loc = []) {
  const idx = loc.length;
  if (idx === r) {
    yield loc;
    return;
  }
  for (let next of range(idx ? last(loc) + 1 : 0, n - r + idx)) { yield* numericCombinations(n, r, loc.concat(next)); }
}
function* combinations(arr, r) {
  for (let idxs of numericCombinations(arr.length, r)) { yield idxs.map(i => arr[i]); }
}
功能*范围(开始、结束){
对于(;开始arr[i]);}
}

您可能想看看@AndreiMatracaru,您能更具体一点吗?你能举个例子吗?:)谢谢检查
array
是否有唯一的元素,或者它可以像
['a'、'b'、'c'、'd'、'a']
那样,结果将是相同的?@bluehipy接受的答案是我想要的,其余的我可以锻炼,但谢谢!)由于安全原因,无法访问该页。你能在别的地方展示一下吗?谢谢!:)美好的不确定为什么这里不是这样:理论上是一样的,但实现与python组合截然不同。
function* range(start, end) {
  for (; start <= end; ++start) { yield start; }
}
function last(arr) { return arr[arr.length - 1]; }
function* numericCombinations(n, r, loc = []) {
  const idx = loc.length;
  if (idx === r) {
    yield loc;
    return;
  }
  for (let next of range(idx ? last(loc) + 1 : 0, n - r + idx)) { yield* numericCombinations(n, r, loc.concat(next)); }
}
function* combinations(arr, r) {
  for (let idxs of numericCombinations(arr.length, r)) { yield idxs.map(i => arr[i]); }
}