【发布时间】:2017-05-09 15:18:48
【问题描述】:
我想知道是否有一种方法可以在哈希表中搜索包含值的键和值。
目前我正在这样做:
#for this example say
bot,input = "rock","scissors"
hash = Hash['rock', 'scissors', 'scissors', 'paper', 'paper', 'rock']
outcome = ([hash.rassoc(bot), hash.assoc(bot)] & [hash.rassoc(input),hash.assoc(input)]).flatten
Outcome 在这里找到所有包含 bot 和 input 的哈希元素;然后检查他们有哪些共同点。 只是想知道我是否有一种方法可以返回我想要的:
outcome = [hash.rassoc(bot), hash.assoc(bot)]
【问题讨论】: