我希望能够比较两个共同的数组,而不是重复数据。
array1 = ['1. walking to the park to find some ice cream']
array2 = ['2. walking to the park to find ice cream and find more ice cream']
示例输出:
walking to the park find ice cream
答案 0 :(得分:7)
a = '1. walking to the park to find some ice cream'
b = '2. walking to the park to find ice cream and find more ice cream'
a1 = a.split(" ")
b1 = b.split(" ")
common = a1 & b1
#=> ["walking", "to", "the", "park", "find", "ice", "cream"]
正则表达式将会变慢。这是数组联合和使用Regexp之间的快速比较:
require 'benchmark'
def arr_only(a,b)
a1 = a.split(" ")
b1 = b.split(" ")
a1 & b1
end
def w_regex(a,b)
match = Regexp.union(a.split(" "))
b.scan(match).uniq
end
n = 100_000
Benchmark.bm do |x|
x.report("arr:") { n.times {|i| arr_only(a, b) } }
x.report("regex:"){ n.times {|i| w_regex(a, b) } }
end
# user system total real
# arr: 1.030000 0.000000 1.030000 ( 1.031033)
# regex: 4.970000 0.010000 4.980000 ( 4.993263)
答案 1 :(得分:1)
我会这样做:
a = '1. walking to the park to find some ice cream'
b = '2. walking to the park to find ice cream and find more ice cream'
match = Regexp.union(a.split(" "))
b.scan(match).uniq.join(" ")
# => "walking to the park find ice cream"