Java中与Python的itertools库中的product函数对应的替代方法是什么?

huangapple go评论116阅读模式
英文:

Java alternative of product function of python form itertools

问题

在我的一个Java程序中,我需要使用itertools提供的product函数,对一个数组进行排列,重复k次。( perm=itertools.product(arr,repeat=k))。

例如,对于arr=[4,5]和k=3,输出应为:

  1. (4, 4, 4)
  2. (4, 4, 5)
  3. (4, 5, 4)
  4. (4, 5, 5)
  5. (5, 4, 4)
  6. (5, 4, 5)
  7. (5, 5, 4)
  8. (5, 5, 5)

我想问一下,在Java中是否有任何实用工具或类似的东西可以简化这个过程?我已经在互联网上寻找过,但无法在任何地方找到。

如果您知道在这种情况下应该如何操作,请分享一些相关信息。

英文:

In one of my java program, I need to use exactly what product function from itertools offers, permuting an array reapeating k times.( perm=itertools.product(arr,repeat=k)).

for ex. for arr=[4,5] and k=3, the output should be:

  1. (4, 4, 4)
  2. (4, 4, 5)
  3. (4, 5, 4)
  4. (4, 5, 5)
  5. (5, 4, 4)
  6. (5, 4, 5)
  7. (5, 5, 4)
  8. (5, 5, 5)

I want to ask if there is any utility or something in java which can facilitate this in java? I have been looking for it over the internet, but couldn't find it anywhere.

Please share something if you know what could be done in this case.

答案1

得分: 3

尝试一下:

我已经使用了 Python 的 itertools.product 代码作为参考。

  1. public class Util {
  2. public static <T> List<Collection<T>> product(Collection<T> a, int r) {
  3. List<Collection<T>> result = Collections.nCopies(1, Collections.emptyList());
  4. for (Collection<T> pool : Collections.nCopies(r, new LinkedHashSet<>(a))) {
  5. List<Collection<T>> temp = new ArrayList<>();
  6. for (Collection<T> x : result) {
  7. for (T y : pool) {
  8. Collection<T> z = new ArrayList<>(x);
  9. z.add(y);
  10. temp.add(z);
  11. }
  12. }
  13. result = temp;
  14. }
  15. return result;
  16. }
  17. public static void main(String[] args) {
  18. product(List.of(4, 5), 3).forEach(System.out::println);
  19. }
  20. }

输出:

  1. [4, 4, 4]
  2. [4, 4, 5]
  3. [4, 5, 4]
  4. [4, 5, 5]
  5. [5, 4, 4]
  6. [5, 4, 5]
  7. [5, 5, 4]
  8. [5, 5, 5]
英文:

Try this:

I have used python itertools.product code as reference.

  1. public class Util {
  2. public static &lt;T&gt; List&lt;Collection&lt;T&gt;&gt; product(Collection&lt;T&gt; a, int r) {
  3. List&lt;Collection&lt;T&gt;&gt; result = Collections.nCopies(1, Collections.emptyList());
  4. for (Collection&lt;T&gt; pool : Collections.nCopies(r, new LinkedHashSet&lt;&gt;(a))) {
  5. List&lt;Collection&lt;T&gt;&gt; temp = new ArrayList&lt;&gt;();
  6. for (Collection&lt;T&gt; x : result) {
  7. for (T y : pool) {
  8. Collection&lt;T&gt; z = new ArrayList&lt;&gt;(x);
  9. z.add(y);
  10. temp.add(z);
  11. }
  12. }
  13. result = temp;
  14. }
  15. return result;
  16. }
  17. public static void main(String[] args) {
  18. product(List.of(4, 5), 3).forEach(System.out::println);
  19. }
  20. }

Output:

  1. [4, 4, 4]
  2. [4, 4, 5]
  3. [4, 5, 4]
  4. [4, 5, 5]
  5. [5, 4, 4]
  6. [5, 4, 5]
  7. [5, 5, 4]
  8. [5, 5, 5]

huangapple
  • 本文由 发表于 2020年8月16日 13:02:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/63433335.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定