Created
February 10, 2012 09:40
-
-
Save ukitazume/1788064 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# data | |
label = %w{aa, bb, cc} | |
v1 = [1, 2, 3] | |
v2 = [11, 22, 33] | |
# use zip | |
label.zip(v1) #= > [["aa,", 1], ["bb,", 2], ["cc", 3]] | |
label.zip(v1, v2) #= > [["aa,", 1, 11], ["bb,", 2, 22], ["cc", 3, 33]] | |
Hash[label.zip(v1)] #= > {"cc"=>3, "bb,"=>2, "aa,"=>1} | |
Hash[label.zip(v1.zip(v2))] #= > {"cc"=>[3, 33], "bb,"=>[2, 22], "aa,"=>[1, 11]} | |
# use transpose and zip | |
Hash[[label, v1].transpose] #= > {"cc"=>3, "bb,"=>2, "aa,"=>1} | |
Hash[[label, v1.zip(v2)].transpose] #= > {"cc"=>[3, 33], "bb,"=>[2, 22], "aa,"=>[1, 11]} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment