Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Perl中的Python itertools.product()等效项_Python_Perl_Itertools - Fatal编程技术网

Perl中的Python itertools.product()等效项

Perl中的Python itertools.product()等效项,python,perl,itertools,Python,Perl,Itertools,在Python中,我可以使用itertools.product,文档中说它是笛卡尔积,相当于嵌套的for循环 它在Perl中的等效性是什么 Python中的一个示例: import itertools opts_list = [["A","B"], ["C","D","E"], ["F","G"]] print list(itertools.product(*opts_list)) 给出: ['A','C','F','A','C','G','A','D','F','A','E','F','A'

在Python中,我可以使用itertools.product,文档中说它是笛卡尔积,相当于嵌套的for循环

它在Perl中的等效性是什么

Python中的一个示例:

import itertools
opts_list = [["A","B"], ["C","D","E"], ["F","G"]]
print list(itertools.product(*opts_list))
给出:

['A','C','F','A','C','G','A','D','F','A','E','F','A','E','G','B','C','G','B','D','D','G','B','E','F','B','E','G']

我会使用的嵌套循环

我会使用的嵌套循环

我最终使用了:

use Math::Cartesian::Product;
cartesian {print "@_\n"} ["A","B"], ["C", "D", "E"], ["F", "G"];
我最终使用了:

use Math::Cartesian::Product;
cartesian {print "@_\n"} ["A","B"], ["C", "D", "E"], ["F", "G"];
我喜欢:

我喜欢:


您应该使用其他答案中引用的CPAN模块之一。但作为一个有趣的练习,我编写了自己的函数来返回任意数量的输入数组引用的笛卡尔积

my $combinations = get_combinations(["A","B"], ["C","D","E"], ["F","G"]);

foreach my $combo (@$combinations) {
  print "@$combo\n";
}

sub get_combinations {
  my @arrays = @_;

  # pre-determine to the total number of combinations
  my $total = 1;
  foreach my $aref (@arrays) {
    # if our array is empty, add 1 undef item
    push(@$aref, undef) unless scalar @$aref;
    $total *= scalar @$aref;
  }

  my $matrix = [];
  my $block_size = $total;
  for (my $col = 0; $col <= $#arrays; $col++) {
    # determine the number of consecutive times to print each item in the column
    $block_size = $block_size / scalar @{$arrays[$col]};

    # fill-in our 2-D array (matrix), one column (input array) at a time
    for (my $row = 0; $row < $total; $row++) {
      my $item_index = int($row / $block_size) % scalar @{$arrays[$col]};
      $matrix->[$row]->[$col] = $arrays[$col]->[$item_index];
    }

  }

  return wantarray ? @$matrix : $matrix;
}

您应该使用其他答案中引用的CPAN模块之一。但作为一个有趣的练习,我编写了自己的函数来返回任意数量的输入数组引用的笛卡尔积

my $combinations = get_combinations(["A","B"], ["C","D","E"], ["F","G"]);

foreach my $combo (@$combinations) {
  print "@$combo\n";
}

sub get_combinations {
  my @arrays = @_;

  # pre-determine to the total number of combinations
  my $total = 1;
  foreach my $aref (@arrays) {
    # if our array is empty, add 1 undef item
    push(@$aref, undef) unless scalar @$aref;
    $total *= scalar @$aref;
  }

  my $matrix = [];
  my $block_size = $total;
  for (my $col = 0; $col <= $#arrays; $col++) {
    # determine the number of consecutive times to print each item in the column
    $block_size = $block_size / scalar @{$arrays[$col]};

    # fill-in our 2-D array (matrix), one column (input array) at a time
    for (my $row = 0; $row < $total; $row++) {
      my $item_index = int($row / $block_size) % scalar @{$arrays[$col]};
      $matrix->[$row]->[$col] = $arrays[$col]->[$item_index];
    }

  }

  return wantarray ? @$matrix : $matrix;
}

这里有一些很好的建议。我相信应该有一个比这更好的元CPAN。我很想自己做一个,但比我更好的人尝试过,但失败了。这里有一些很好的建议。我相信应该有一个比这更好的元CPAN。我很想自己做一个,但比我更好的人尝试过,但失败了。这个解决方案的优点是它的灵活性。这个解决方案的优点是它的灵活性。