Merge array and hash in ruby if key appears in array
I have two arrays one = [1,2,3,4,5,6,7] and two = [{1=>'10'},{3=>'22'},{7=>'40'}]
Two will have one.length hashes or less. I want a new array of values from two if it's key appears in one, if not then use 0. The new array would be
[10,0,22,0,0,0,40] What is the best way to do this?
I'd do it using Enumerable#reduce and Hash#values_at:
two.reduce({}, :merge).values_at(*one).map(&:to_i)
# => [10, 0, 22, 0, 0, 0, 40]
h = [{1 => '10'}, {3 => '22'}, {7 => '40'}].inject(:merge)
one.map{|e| h[e].to_i}
# => [10, 0, 22, 0, 0, 0, 40]Merge array and hash in ruby if key appears in array的更多相关文章
- Sql优化(一) Merge Join vs. Hash Join vs. Nested Loop
原创文章,首发自本人个人博客站点,转载请务必注明出自http://www.jasongj.com Nested Loop,Hash Join,Merge Join介绍 Nested Loop: 对于被 ...
- YUI Array 之hash
hash就是把两个参数合并成一个类似hashMap结构的对象,用第一个数组的元素为key,第二个的为value,如果第二个参数未指定,则把对象的对应的值置为true YUI原码 YUI hashYAr ...
- 1月24日 ruby基础3部分 Numeric, Array已学。
<div style="background:lightblue"> 第12章 数值类 12.1 数值的构成 Numeric-> Integer-> Fix ...
- PHP中Array的hash函数实现
PHP中使用最多的非Array莫属了,那Array是如何实现的? 在PHP内部Array通过一个hashtable来实现,其中使用链接法解决hash冲突的问题,这样最坏情况下,查找Array元素的复杂 ...
- JavaScript基础精华03(String对象,Array对象,循环遍历数组,JS中的Dictionary,Array的简化声明)
String对象(*) length属性:获取字符串的字符个数.(无论中文字符还是英文字符都算1个字符.) charAt(index)方法:获取指定索引位置的字符.(索引从0开始) indexOf(‘ ...
- Merge join、Hash join、Nested loop join对比分析
简介 我们所常见的表与表之间的Inner Join,Outer Join都会被执行引擎根据所选的列,数据上是否有索引,所选数据的选择性转化为Loop Join,Merge Join,Hash Join ...
- 浅谈SQL Server中的三种物理连接操作(Nested Loop Join、Merge Join、Hash Join)
简介 在SQL Server中,我们所常见的表与表之间的Inner Join,Outer Join都会被执行引擎根据所选的列,数据上是否有索引,所选数据的选择性转化为Loop Join,Merge J ...
- SQL Tuning 基础概述06 - 表的关联方式:Nested Loops Join,Merge Sort Join & Hash Join
nested loops join(嵌套循环) 驱动表返回几条结果集,被驱动表访问多少次,有驱动顺序,无须排序,无任何限制. 驱动表限制条件有索引,被驱动表连接条件有索引. hints:use_n ...
- perl push an array to hash
#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my @array=qw /fm1 fm2 fm3 fm4 fm5 fm6/; ...
随机推荐
- IO初步,字节输入流和字节输出流
字节输出流 OutputStream(基类,抽象) 特点:写任意的文件 方法:写出数据的方法:write write(int b) 写出1个字节 -128~127之间,写的是一个ASCLL码的值 wr ...
- Vue基础(1)
目录 Vue基础 基础 导入 1. 挂载 2. 插值表达式 3. 事件 4. 创建对象 5. v-text和v-html 6. vue的过滤器 7. 属性指令 Vue基础 基础 首先我们要知道Vue是 ...
- js input框限制输入为数字并限制长度
<input type="number" name="price" id="priceVal" placeholder="请 ...
- 2.初始化spark
参考: RDD programming guide http://spark.apache.org/docs/latest/rdd-programming-guide.html SQL progr ...
- Java虚拟机性能管理神器 - VisualVM(8) 查找JAVA应用程序耗时的方法函数【转】
Java虚拟机性能管理神器 - VisualVM(8) 查找JAVA应用程序耗时的方法函数[转] 标签: javajvm监控工具性能优化 2015-04-07 16:47 1846人阅读 评论(0) ...
- PostgreSQL:COALESCE函数
COALESCE函数是返回参数中的第一个非null的值,它要求参数中至少有一个是非null的,如果参数都是null会报错. select COALESCE(null,null); //报错 selec ...
- leetcode-154-寻找旋转排序数组中的最小值
题目描述: 方法一: class Solution: def findMin(self, nums: List[int]) -> int: left, right = 0, len(nums) ...
- CSS - 选择器相关
1. 标签选择器 /* 标签选择器 : 会将样式作用在当前网页所有的指定标签上 标签名 { 样式名1: 样式值1; 样式名2: 样式值2; ...... } */ table { width: 300 ...
- Apache Flink 进阶入门(二):Time 深度解析
前言 Flink 的 API 大体上可以划分为三个层次:处于最底层的 ProcessFunction.中间一层的 DataStream API 和最上层的 SQL/Table API,这三层中的每一层 ...
- VS2010-MFC(对话框:创建对话框类和添加控件变量)
转自:http://www.jizhuomi.com/software/153.html 前两讲中讲解了如何创建对话框资源.创建好对话框资源后要做的就是生成对话框类了.生成对话框类主要包括新建对话框类 ...