如何通过一组新的给定键来更改散列的所有键 [英] How to change all the keys of a hash by a new set of given keys

查看:83
本文介绍了如何通过一组新的给定键来更改散列的所有键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



有没有一种方法可以做到这一点?

解决方案

我假设你想更改散列而不更改值:

  hash = {
nr=>123,
name=>Herrmann ,$ A $,
pferd=>010 000 777,
land=>hight land
} bb,cc,dd]
new_hash = header.zip(hash.values).to_h

结果:

  {
aa=>123,
bb=>Herrmann Hofreiter,
cc=>010 000 777,
dd=>高地
}


How do I change all the keys of a hash by a new set of given keys?

Is there a way to do that elegantly?

解决方案

i assume you want to change the hash keys without changing the values:

hash = {
   "nr"=>"123",
   "name"=>"Herrmann Hofreiter",
   "pferd"=>"010 000 777",
   "land"=>"hight land"
}
header = ["aa", "bb", "cc", "dd"]
new_hash = header.zip(hash.values).to_h

Result:

{
   "aa"=>"123",
   "bb"=>"Herrmann Hofreiter",
   "cc"=>"010 000 777",
   "dd"=>"high land"
}

这篇关于如何通过一组新的给定键来更改散列的所有键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆