ruby 将两个数组合并为哈希

yvt65v4c  于 2023-02-03  发布在  Ruby
关注(0)|答案(8)|浏览(199)

我有两个数组:

members     = ["Matt Anderson", "Justin Biltonen", "Jordan Luff", "Jeremy London"]
instruments = ["guitar, vocals", "guitar", "bass", "drums"]

我想做的是把这些合并起来,这样得到的数据结构就是一个哈希值,如下所示:

{"Matt Anderson"=>["guitar", "vocals"], "Justin Biltonen"=>"guitar", "Jordan Luff"=>"bass", "Jeremy London"=>"drums"}

注意“安德森”的值现在是一个数组而不是字符串,有Ruby向导愿意试试吗?
我知道Hash[*members.zip(instruments).flatten]几乎以我想要的方式组合了它们,但是先把“吉他,人声”字符串变成一个数组怎么样?谢谢。

kg7wmglp

kg7wmglp1#

正如雷夫凯特勒张贴,使用zip是要走的路。

Hash[members.zip(instruments)]
8nuwlpux

8nuwlpux2#

使用mapsplit将乐器字符串转换为数组:

instruments.map {|i| i.include?(',') ? (i.split /, /) : i}

然后使用Hash[]zipmembersinstruments组合:

Hash[members.zip(instruments.map {|i| i.include?(',') ? (i.split /, /) : i})]

得到

{"Jeremy London"=>"drums",
 "Matt Anderson"=>["guitar", "vocals"],
 "Jordan Luff"=>"bass",
 "Justin Biltonen"=>"guitar"}

如果你不关心单项列表是否也是数组,你可以使用这个简单的解决方案:

Hash[members.zip(instruments.map {|i| i.split /, /})]

它会给你这个

{"Jeremy London"=>["drums"],
 "Matt Anderson"=>["guitar", "vocals"],
 "Jordan Luff"=>["bass"],
 "Justin Biltonen"=>["guitar"]}
hxzsmxv2

hxzsmxv23#

示例01

k = ['a', 'b', 'c']
v = ['aa', 'bb']
h = {}

k.zip(v) { |a,b| h[a.to_sym] = b } 
# => nil

p h 
# => {:a=>"aa", :b=>"bb", :c=>nil}

示例02

k = ['a', 'b', 'c']
v = ['aa', 'bb', ['aaa','bbb']]
h = {}

k.zip(v) { |a,b| h[a.to_sym] = b }
p h 
# => {:a=>"aa", :b=>"bb", :c=>["aaa", "bbb"]}
mhd8tkvw

mhd8tkvw4#

这是做你想做的事情的最好和最干净的方法。

Hash[members.zip(instruments.map{|i| i.include?(',') ? i.split(',') : i})]

好好享受吧!

bfnvny8b

bfnvny8b5#

h = {}
members.each_with_index do |el,ix|
    h[el] = instruments[ix].include?(",") ? instruments[ix].split(",").to_a : instruments[ix]
end
h
nxowjjhe

nxowjjhe6#

members.inject({}) { |m, e| t = instruments.delete_at(0).split(','); m[e] = t.size > 1 ? t : t[0]; m }

如果不关心结果中的单元素数组,可以用途:

members.inject({}) { |m, e| m[e] = instruments.delete_at(0).split(','); m }
nfeuvbwi

nfeuvbwi7#

h = {}

members.each_with_index {|item, index|
     h.store(item,instruments[index].split)
}
omhiaaxx

omhiaaxx8#

当处理这样的编程任务时,从逐步数据转换的Angular 来考虑会有所帮助。
您有两个数组:乐队成员和他们演奏的乐器。但有时一个成员演奏不止一种乐器。这意味着有另一个实体在暗中潜伏。
这个实体可以通过引入一个逻辑条件来显式化:

if member plays multiple parts:
  return an array containing those parts
 else:
  return the instrument

根据这个条件转换了原始的instruments数组之后,就可以将其合并为一个成员为以下成员的哈希:

members     = ["Matt Anderson", "Justin Biltonen", "Jordan Luff", "Jeremy London"]
instruments = ["guitar, vocals", "guitar", "bass", "drums"]

def band(members, instruments)
 roles = instruments.map do |ins|
  arr = ins.split(", ")
  arr.length > 1 ?  arr : ins
 end
 Hash[members.zip(roles)]
end

require 'minitest/spec'
require 'minitest/autorun'

describe "creates a band out of members and instruments" do
 it "combines arrays into a hash" do

  band(members, instruments).must_equal({
     "Matt Anderson"   => ["guitar", "vocals"],
     "Justin Biltonen" => "guitar",
     "Jordan Luff"     => "bass",
     "Jeremy London"   => "drums"
    })

 end
end

相关问题