在Ruby中将字符串数组转换为哈希值

时间:2022-05-22 21:43:50

I have an Array that contains strings:

我有一个包含字符串的数组:

["First Name", "Last Name", "Location", "Description"]

[“名字”,“姓氏”,“位置”,“描述”]

I need to convert the Array to a Hash, as in the following:

我需要将Array转换为Hash,如下所示:

{"A" => "First Name", "B" => "Last Name", "C" => "Location", "D" => "Description"}

{“A”=>“名字”,“B”=>“姓氏”,“C”=>“位置”,“D”=>“描述”}

Also, this way too:

这也是这样的:

{"First Name" => "A", "Last Name" => "B", "Location" => "C", "Description" => "D"}

{“名字”=>“A”,“姓氏”=>“B”,“位置”=>“C”,“描述”=>“D”}

Any thoughts how to handle this the best way?

有什么想法如何处理这个最好的方法?

4 个解决方案

#1


1  

You could do this:

你可以这样做:

arr = ["First Name", "Last Name", "Location", "Description"]

letter = Enumerator.new do |y|
  l = ('A'.ord-1).chr
  loop do
    y.yield l=l.next
  end
end
  #=> #<Enumerator: #<Enumerator::Generator:0x007f9a00878fd8>:each> 

h = arr.each_with_object({}) { |s,h| h[letter.next] = s }
  #=> {"A"=>"First Name", "B"=>"Last Name", "C"=>"Location", "D"=>"Description"} 

h.invert
  #=> {"First Name"=>"A", "Last Name"=>"B", "Location"=>"C", "Description"=>"D"} 

or

要么

letter = ('A'.ord-1).chr
  #=> "@" 
h = arr.each_with_object({}) { |s,h| h[letter = letter.next] = s }
  #=> {"A"=>"First Name", "B"=>"Last Name", "C"=>"Location", "D"=>"Description"} 

When using the enumerator letter, we have

使用调查员信时,我们有

27.times { puts letter.next }
  #=> "A"
  #   "B"
  #   ...
  #   "Z"
  #   "AA"

#2


2  

You could implement as follows

您可以按如下方式实施

 def string_array_to_hash(a=[],keys=false)
    headers = ("A".."Z").to_a
    Hash[keys ? a.zip(headers.take(a.count)) : headers.take(a.count).zip(a)]
 end

Then to get your initial output it would be

然后得到你的初始输出

 a = ["First Name", "Last Name", "Location", "Description"]
 string_array_to_hash a 
 #=> {"A"=>"First Name", "B"=>"Last Name", "C"=>"Location", "D"=>"Description"}

And second output is

第二个输出是

 a = ["First Name", "Last Name", "Location", "Description"]
 string_array_to_hash a, true 
 #=> {"First Name"=>"A", "Last Name"=>"B", "Location"=>"C", "Description"=>"D"}

Note: this will work as long as a is less than 27 Objects otherwise you will have to specify a different desired output. This is due to the fact that a) the alphabet only has 26 letters b) Hash objects can only have unique keys.

注意:只要a小于27个对象,这将起作用,否则您将必须指定不同的所需输出。这是因为a)字母表只有26个字母b)哈希对象只能有唯一的键。

#3


0  

If you are not being specific about keys name then you could try this out

如果您没有具体说明密钥名称,那么您可以试试这个

 list = ["First Name", "Last Name", "Location", "Description"] 
 Hash[list.map.with_index{|*x|x}].invert

Output

产量

 {0=>"First Name", 1=>"Last Name", 2=>"Location", 3=>"Description"} 

Similar solutions is here.

类似的解决方案在这里

#4


0  

Or..You also can try this :)

或..你也可以尝试这个:)

letter = 'A'
arr = ["First Name", "Last Name", "Location", "Description"]
hash = {}
arr.each { |i|
hash[i] = letter
letter = letter.next
}

// => {"First Name"=>"A", "Last Name"=>"B", "Location"=>"C", "Description"=>"D"} 

or

要么

letter = 'A'
arr = ["First Name", "Last Name", "Location", "Description"]
hash = {}
arr.each { |i|
hash[letter] = i
letter = letter.next
}

// => {"A"=>"First Name", "B"=>"Last Name", "C"=>"Location", "D"=>"Description"} 

#1


1  

You could do this:

你可以这样做:

arr = ["First Name", "Last Name", "Location", "Description"]

letter = Enumerator.new do |y|
  l = ('A'.ord-1).chr
  loop do
    y.yield l=l.next
  end
end
  #=> #<Enumerator: #<Enumerator::Generator:0x007f9a00878fd8>:each> 

h = arr.each_with_object({}) { |s,h| h[letter.next] = s }
  #=> {"A"=>"First Name", "B"=>"Last Name", "C"=>"Location", "D"=>"Description"} 

h.invert
  #=> {"First Name"=>"A", "Last Name"=>"B", "Location"=>"C", "Description"=>"D"} 

or

要么

letter = ('A'.ord-1).chr
  #=> "@" 
h = arr.each_with_object({}) { |s,h| h[letter = letter.next] = s }
  #=> {"A"=>"First Name", "B"=>"Last Name", "C"=>"Location", "D"=>"Description"} 

When using the enumerator letter, we have

使用调查员信时,我们有

27.times { puts letter.next }
  #=> "A"
  #   "B"
  #   ...
  #   "Z"
  #   "AA"

#2


2  

You could implement as follows

您可以按如下方式实施

 def string_array_to_hash(a=[],keys=false)
    headers = ("A".."Z").to_a
    Hash[keys ? a.zip(headers.take(a.count)) : headers.take(a.count).zip(a)]
 end

Then to get your initial output it would be

然后得到你的初始输出

 a = ["First Name", "Last Name", "Location", "Description"]
 string_array_to_hash a 
 #=> {"A"=>"First Name", "B"=>"Last Name", "C"=>"Location", "D"=>"Description"}

And second output is

第二个输出是

 a = ["First Name", "Last Name", "Location", "Description"]
 string_array_to_hash a, true 
 #=> {"First Name"=>"A", "Last Name"=>"B", "Location"=>"C", "Description"=>"D"}

Note: this will work as long as a is less than 27 Objects otherwise you will have to specify a different desired output. This is due to the fact that a) the alphabet only has 26 letters b) Hash objects can only have unique keys.

注意:只要a小于27个对象,这将起作用,否则您将必须指定不同的所需输出。这是因为a)字母表只有26个字母b)哈希对象只能有唯一的键。

#3


0  

If you are not being specific about keys name then you could try this out

如果您没有具体说明密钥名称,那么您可以试试这个

 list = ["First Name", "Last Name", "Location", "Description"] 
 Hash[list.map.with_index{|*x|x}].invert

Output

产量

 {0=>"First Name", 1=>"Last Name", 2=>"Location", 3=>"Description"} 

Similar solutions is here.

类似的解决方案在这里

#4


0  

Or..You also can try this :)

或..你也可以尝试这个:)

letter = 'A'
arr = ["First Name", "Last Name", "Location", "Description"]
hash = {}
arr.each { |i|
hash[i] = letter
letter = letter.next
}

// => {"First Name"=>"A", "Last Name"=>"B", "Location"=>"C", "Description"=>"D"} 

or

要么

letter = 'A'
arr = ["First Name", "Last Name", "Location", "Description"]
hash = {}
arr.each { |i|
hash[letter] = i
letter = letter.next
}

// => {"A"=>"First Name", "B"=>"Last Name", "C"=>"Location", "D"=>"Description"}