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
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 n
[Daily Coding Problem] 1 (LeetCode 1). Find if two numbers in an array add up to k
This problem was recently asked by Google. Given a list of numbers and a number k, return whether any two numbers from the list add up to&n
question 3 of Nowcode : write a array and return the object in it of the reverse direction
function printListFromTailToHead(head) { var curObj = head; //curObj is the pointer that points the head var Arr = []; while (curOb
Merge Sort Array and Merge Sort Linked List
Merge Sort Array: 看完stanford的 CS106B的video,https://www.youtube.com/watch?v=LlNawf0JeF0&list=PLnfg8b9vdpLn9exZweTJx44CII1bYczuk&index=55 
Evolving AI Will Play Key Role In Tech Hiring And Firing
In this guest column, Adam Rogers, chief technology officer of Ultimate Software, describes where he believes AI and machine learning will lead HCM technol
Array And String(數組和字符串)
每一個 重新 存儲 pack 分享 nbsp ret 數據結構 con 1.實現一個算法,確定一個字符串的所有字符是否全都不同。假使不允許使用額外的數據結構,又該怎麽處理? public class UniqueChars { public static vo
“上下文關鍵字”KWIC(Key Word in Context,文本中的關鍵字)檢索系統嘗試
運行結果截圖 lob break 依據 關鍵字 () 命令行 int 重復 一、實驗目的 1.理解不同體系結構風格的具體內涵。 2.學習體系結構風格的具體實踐。 二、實驗環境 硬件: (依據具體情況填寫) 軟件:Java或任何一種自己熟悉的語言 三、實驗內容 “上下文關鍵字
[AngularJS] Store the entry url and redirect to entry url after Logged in
logged in ice angular his can tps to do this cat For example when a outside application need to visit your app address: https://www.ex
手機屏缺陷檢測《Scale insensitive and focus driven mobile screen defect detection in industry》
文章連結:https://ac.els-cdn.com/S0925231218302996/1-s2.0-S0925231218302996-main.pdf?_tid=e6efda84-db95-47f2-8203-7a407e4f7265&acdnat=1540366950_8c37c8
Python學習(七):生成器表示式(expr for iner_var in iterable if cond_expr)
列表解析:[expr for iter_var in iterable if cond_expr] 生成器表示式:(expr for iter_var in iterable if cond_expr) J = 'aadsjnk' S = 'asadasbxjs
Weekly Contest 78-------->811. Subdomain Visit Count (split string with space and hash map)
A website domain like "discuss.leetcode.com" consists of various subdomains. At the top level, we have "com", at the next level, we have "leetcode.com", an
Failed to find target with hash string 'android-26' in
是因為 專案使用 Android 26 但是 SDK並沒有安裝,使用了其他版本 參考 https://blog.csdn.net/ThePromonkeyOf_HeLuo/article/details/78518701 去SDK Manager中下載相應的api,之後從新編譯即可。新版Android
Failed to find target with hash string 'android-26' in
是因為 專案使用 Android 26 但是 SDK並沒有安裝,使用了其他版本 參考 https://blog.csdn.net/ThePromonkeyOf_HeLuo/article/details/78518701 去SDK Manager中下載相應的api,之後從新編譯即可。新版
LeetCode in Python: Remove Duplicates from Sorted Array II
LeetCode in Python Remove Duplicates from Sorted Array II class Solution: def RemoveDuplicatesFromSortedArrayII(self, nums): if le
LeetCode in Python: Remove Duplicates from Sorted Array
LeetCode in Python Remove Duplicates from Sorted Array (Easy) class Solution: def removeDuplicates(self, nums): """ :type
CF498C Array and Operations (質因數分解+最大流)
題目連結 qwq 最近突然想做網路流相關的整理啊 QWQ其實就是之前一段時間做的網路流的題 然後拿出來整理一下(這道並不是) 首先,我們很容易發現這個題目中,對於每一種關係,一定是除一個質因數是最優秀的。因為這樣可以保證你除的次數儘可能的多。 那麼我們首先第一步就是把所有的數都質
Merge Join vs. Hash Join vs. Nested Loop
Nested Loop,Hash Join,Merge Join介紹 Nested Loop: 對於被連線的資料子集較小的情況,Nested Loop是個較好的選擇。Nested Loop就是掃描一個表(外表),每讀到一條記錄,就根據Join欄位上的索引去另一張表(內表)裡面查詢,若Join欄位上沒
How to Enable and Use Windows 10’s New Built-in SSH Commands
Microsoft announced it was bringing an integrated OpenSSH client to Windows in 2015. They’ve finally done it, and an SSH client is hidden
[iOS] Check key exists in NSDictionary in Objective-C
把 json 轉成 dictionary 後,想檢檢視看某一個 key 是不是存在。 objectForKey will return nil if a key doesn’t exist. ex: if([obj valueForKey:@"SetEntries"] != nil) { //
How to cover artificial intelligence and understand its impact on journalism: MOOC in Spanish, in partnership with Microsoft
The term "artificial intelligence" has been around since 1956, and yet many journalists are unfamiliar with its history and impact on the world today, even