Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 给定两个数组,将它们二元重叠的最有效方法是什么?_Arrays_Ruby - Fatal编程技术网

Arrays 给定两个数组,将它们二元重叠的最有效方法是什么?

Arrays 给定两个数组,将它们二元重叠的最有效方法是什么?,arrays,ruby,Arrays,Ruby,鉴于: 我正在寻找最有效的方法来实现这一点: a = ["thing1", "thing2", "thing3", "thing4", "thing5", "thing6"] b = ["thing3", "thing4", "thing5"] 一定有比^2更好的方法 result #=> [0, 0, 1, 1, 1, 0] 集合是通过一个隐藏的散列来实现的,因此集合查找在执行速度方面与散列查找类似 集合是用一个隐藏的散列来实现的,因此集合查找在执行速度方面与散列查找类似。或者有了主

鉴于:

我正在寻找最有效的方法来实现这一点:

a = ["thing1", "thing2", "thing3", "thing4", "thing5", "thing6"]
b = ["thing3", "thing4", "thing5"]
一定有比^2更好的方法

result #=> [0, 0, 1, 1, 1, 0]
集合是通过一个隐藏的散列来实现的,因此集合查找在执行速度方面与散列查找类似


集合是用一个隐藏的散列来实现的,因此集合查找在执行速度方面与散列查找类似。

或者有了主动支持,c=b.index\u by&:我注意到您总是喜欢使用新方法或现有方法的新功能,这是一件好事,因为它具有教育价值。在这里,我被允许在Ruby v2.6中有一个可选块。嗨,sawa,我看过你的个人资料,你说你将很快发布与Web服务器相关的gem。我能告诉你什么时候可以吗?@Rajagopalan我在链接页面下的内容是allI现在提供的,并且该个人资料是旧的,尚未更新。抱歉。@sawa我可以知道您的web服务工作的链接在哪里吗?或者在积极支持下,c=b.index_by&:我注意到您总是喜欢使用新方法或现有方法的新功能,这是一件好事,因为它具有教育价值。在这里,我被允许在Ruby v2.6中有一个可选块。嗨,sawa,我看过你的个人资料,你说你将很快发布与Web服务器相关的gem。我能告诉你什么时候可以吗?@Rajagopalan我在链接页面下的内容是allI现在提供的,并且该个人资料是旧的,尚未更新。抱歉。@sawa我可以知道您的web服务工作的链接在哪里吗?这里的关键是索引b将其转换为散列,这样在循环a之前可以得到O1查找。这使它准时。这里的关键是索引b将其转换为散列,这样在循环a之前可以得到O1查找。这使它准时
a.map{|v| b.include?(v) ? 1 : 0}
c = b.to_h{|e| [e, true]}
a.map{|e| c[e] ? 1 : 0}
require 'set'

bs = b.to_set
  #=> #<Set: {"thing3", "thing4", "thing5"}> 
a.map { |s| bs.include?(s) ? 1 : 0 }
  #=> [0, 0, 1, 1, 1, 0]