多个数组的元素组合

时间:2016-08-08 17:27:04

标签: arrays ruby

我正在从事化学研究项目,我需要创建各种角度的文件。我想要以下各项的组合:

angle1 can be from [0, -36, -72, -108, -144, -180]
angle2 can be from [-180, -108, -36]
angle3 can be from [0, -36, -72, -108, -144, -180]
angle4 can be from [-180, -108, -36]

我为此编写了一些Ruby代码,但它似乎只给了我预期组合数量的一半。我的编程技巧不是很好,所以我想知道是否有人能告诉我我做错了什么。

非常感谢您提供的任何帮助:

phi1 = [0, -36, -72, -108, -144, -180]
psi1 = [-180, -108, -36]

phi2 = [0, -36, -72, -108, -144, -180]
psi2 = [-180, -108, -36]

psi1.each do |a|
  psi2.each do |b|
    phi1.each do |c|
      psi2.each do |d|

        line1 = 'select' + "#{b}" + '}}'
        line2 = 'select' + "#{a}" + '}}'
        line3 = 'select' + "#{d}" + '}}'
        line4 = 'select' + "#{c}" + '}}'

        filename = "angles#{b}_#{a}_#{d}_#{c}"
        puts filename
        puts line1
        puts line2
        puts line3
        puts line4
      end
    end
  end
end

预期输出将是'puts filename',其中filename将是phi1,psi1,phi2,psi2的每个组合。我预计它会增加324次,但它只会增加162次。

3 个答案:

答案 0 :(得分:3)

您应该在这里使用Array#product

phi1 = [0, -36, -72, -108, -144, -180]
psi1 = [-180, -108, -36]
phi2 = [0, -36, -72, -108, -144, -180]
psi2 = [-180, -108, -36]

phi1.product(psi1, phi2, psi2).each do |arr|
  puts "angles#{ arr.join("_") }"
  arr.each { |angle| puts "select #{angle}" }
end

angles0_-180_0_-180
select 0
select -180
select 0
select -180
angles0_-180_0_-108
select 0
select -180
select 0
select -108
...
angles-180_-36_-180_-108
select -180
select -36
select -180
select -108
angles-180_-36_-180_-36
select -180
select -36
select -180
select -36

phi1.product(psi1, phi2, psi2).count
  #=> 324

答案 1 :(得分:0)

我相信你想要这样的东西。你的问题是你在同一个数组中覆盖了值。

phi1 = [0, -36, -72, -108, -144, -180]
psi1 = [-180, -108, -36]

phi2 = [0, -36, -72, -108, -144, -180]
psi2 = [-180, -108, -36]

如果您更改了应该解决问题的第二个数组的名称,我输入了我想要的内容。

phi1 = [0, -36, -72, -108, -144, -180]
psi3 = [-180, -108, -36]

phi2 = [0, -36, -72, -108, -144, -180]
psi4 = [-180, -108, -36]

psi1.each do |a|
  psi2.each do |b|
    phi3.each do |c|
      psi4.each do |d|

        line1 = 'select' + "#{b}" + '}}'
        line2 = 'select' + "#{a}" + '}}'
        line3 = 'select' + "#{d}" + '}}'
        line4 = 'select' + "#{c}" + '}}'

        filename = "angles#{b}_#{a}_#{d}_#{c}"
        puts filename
        puts line1
        puts line2
        puts line3
        puts line4
     end
    end
  end
end

答案 2 :(得分:0)

为什么要两次声明phi1phi2?我会做这样的事情:

phi1 = [0, -36, -72, -108, -144, -180]

phi2 = [-180, -108, -36]

phi3 = [0, -36, -72, -108, -144, -180] # this one now ends in 3

phi4 = [-180, -108, -36] # this one now ends in 4

# I declared a new, unique array for every set of possible 
# angle values

phi1.each do |a|
  phi2.each do |b|
    phi3.each do |c|
      phi4.each do |d| #I also made it so the for loops are for each individual phi 
                       #array.

        line1 = 'select' + "#{b}" + '}}'
        line2 = 'select' + "#{a}" + '}}'
        line3 = 'select' + "#{d}" + '}}'
        line4 = 'select' + "#{c}" + '}}'

        filename = "angles#{b}_#{a}_#{d}_#{c}"
        puts filename
        puts line1
        puts line2
        puts line3
        puts line4
      end
    end
  end
end

我希望这会有所帮助。