简体   繁体   English

如何将数组中的每个值 map 转换为其他数组中存在的每个值? [关闭]

[英]How to map each value in an array to each value present in other array? [closed]

I'm pretty new to programming & Ruby. Any teachings on this one would be appreciated.我对编程和 Ruby 很陌生。任何关于这方面的教学都将不胜感激。

I have two arrays. The values in arrays are dynamic and can be changed based on what user inputs, eg:我有两个 arrays。arrays 中的值是动态的,可以根据用户输入的内容进行更改,例如:

name = [john, vick, nicki]
place = [usa, france]

I want to create a hash map with each value in the name array mapped to each value in tge place array.我想创建一个 hash map,名称数组中的每个值都映射到 tge 位置数组中的每个值。

I'm expecting this output:我期待这个 output:

Hash = { "john" => "usa", "john" => "france", "vick => usa", "vick" => "france" "nicki" => "usa", "nicki" => "france" }

How can I achieve this?我怎样才能做到这一点?

To map each value in one array to each value present in another array in Ruby, you can use the following method:要将一个数组中的每个值映射到 Ruby 中另一个数组中存在的每个值,可以使用以下方法:

Create two arrays, for example:创建两个数组,例如:

names = ["john", "vick", "nicki"]
places = ["usa", "france"]

Use the Array#product method to combine the two arrays into a new array of all possible pairs:使用 Array#product 方法将两个数组组合成一个包含所有可能对的新数组:

pairs = names.product(places)
# => [["john", "usa"], ["john", "france"], ["vick", "usa"], ["vick", "france"], ["nicki", "usa"], ["nicki", "france"]]

Use the Enumerable#each_with_object method to iterate over the pairs array and add each pair to a new hash:使用 Enumerable#each_with_object 方法遍历 pairs 数组并将每一对添加到新的散列中:

hash = pairs.each_with_object({}) do |(name, place), h|
  h[name] = place
end
# => {"john"=>"usa", "vick"=>"usa", "nicki"=>"usa"}

This will create a new hash with the following values:这将创建一个具有以下值的新散列:

{"john"=>"usa", "vick"=>"usa", "nicki"=>"usa"}

Note that in this example, the final value for each key in the hash will be the last value in the places array.请注意,在此示例中,散列中每个键的最终值将是 places 数组中的最后一个值。 If you want to map all possible pairs of values, you can use the Hash#merge method to add each pair to the hash, like this:如果要映射所有可能的值对,可以使用 Hash#merge 方法将每一对添加到散列中,如下所示:

hash = pairs.each_with_object({}) do |(name, place), h|
  h.merge!(name => place)
end
# => {"john"=>"france", "vick"=>"france", "nicki"=>"france"}

This will create a new hash with the following values:这将创建一个具有以下值的新散列:

{"john"=>"france", "vick"=>"france", "nicki"=>"france"}

There are duplicated keys, such as john.有重复的键,例如 john。 A hash has unique keys and can have multiple values.散列具有唯一键并且可以有多个值。

You could use an array in the hash, such as:您可以在散列中使用数组,例如:

result = { "john" => ["usa", "france"], "vick => ["usa", "france"]... }

Here's an easy example to fill create a given result hash (each element is equal to the place array):这是一个简单的示例,用于填充创建给定的结果哈希(每个元素等于位置数组):

# name = ['john', 'vick', 'nicki']
place = ['usa', 'france']

# set up final hash, other options available.
result = {}

# loop to create the hash with names and arrays
name.each do |n|
  result[n] = place
end

# output the final hash
puts result

There's a predefined method to_h to cast to hash.有一个预定义的方法to_h可以转换为散列。

Used on name and place gives用于nameplace

name.collect {|i| [i, place]}.to_h
{"john"=>["usa", "france"], "vick"=>["usa", "france"], "nicki"=>["usa", "france"]}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM