Return 个与 Ruby 相似的数组元素
Return similar elements of array in Ruby
假设我有这样一个数组:
arr = ['footballs_jumba_10', 'footballs_jumba_11', 'footballs_jumba_12',
'footballs_jumba_14', 'alpha_romeo_11', 'alpha_romeo_12',
'alpha_juliet_10', 'alpha_juliet_11']
如果我想 return 重复,(假设数组中的任何这些字符串完全相同,我会
return arr.detect{ |a| arr.count(a) > 1 }
但是,如果我只想获取数组每个元素的前 10 个字符的副本,而事先不知道变化,该怎么办?像这样:
['footballs_', 'alpha_rome', 'alpha_juli']
使用Array#uniq
:
arr.map {|e| e[0..9]}.uniq
# => ["footballs_", "alpha_rome", "alpha_juli"]
我在回答 here:
中提出的 Arry#difference
方法非常简单
arr << "Let's add a string that appears just once"
#=> ["footballs_jumba_10", "footballs_jumba_11", "footballs_jumba_12",
# "footballs_jumba_14", "alpha_romeo_11", "alpha_romeo_12",
# "alpha_juliet_10", "alpha_juliet_11", "Let's add a string that appears just once"]
a = arr.map { |s| s[0,10] }
#=> ["footballs_", "footballs_", "footballs_", "footballs_", "alpha_rome",
# "alpha_rome", "alpha_juli", "alpha_juli", "Let's add "]
b = a.difference(a.uniq)
#=> ["footballs_", "footballs_", "footballs_", "alpha_rome", "alpha_juli"]
b.uniq
#=> ["footballs_", "alpha_rome", "alpha_juli"]
你可以这样做:
def partial_duplicates(elements)
unique = {}
duplicates = {}
elements.each do |e|
partial = e[0..9]
# If the element is in the hash, it is a duplicate.
if first_element = unique[partial]
duplicates[first_element] = true
duplicates[e] = true
else
# include the element as unique
unique[partial] = e
end
end
duplicates.keys
end
这将 return 个独特的重复项。如果你想要所有重复项,你可以只使用 Array
.
此外,这个 return 是每个副本的所有完整表示,因为它看起来更有用,可能是您想要的:
partial_duplicates(arr)
=> ["footballs_jumba_10", "footballs_jumba_11", "footballs_jumba_12", "footballs_jumba_14", "alpha_romeo_11", "alpha_romeo_12", "alpha_juliet_10", "alpha_juliet_11"]
如果您只需要部分重复项,您可以将条件更改为:
if unique[partial]
duplicates[partial] = true
else
unique[partial] = true
end
然后:
partial_duplicates(arr)
=> ["footballs_", "alpha_rome", "alpha_juli"]
假设我有这样一个数组:
arr = ['footballs_jumba_10', 'footballs_jumba_11', 'footballs_jumba_12',
'footballs_jumba_14', 'alpha_romeo_11', 'alpha_romeo_12',
'alpha_juliet_10', 'alpha_juliet_11']
如果我想 return 重复,(假设数组中的任何这些字符串完全相同,我会
return arr.detect{ |a| arr.count(a) > 1 }
但是,如果我只想获取数组每个元素的前 10 个字符的副本,而事先不知道变化,该怎么办?像这样:
['footballs_', 'alpha_rome', 'alpha_juli']
使用Array#uniq
:
arr.map {|e| e[0..9]}.uniq
# => ["footballs_", "alpha_rome", "alpha_juli"]
我在回答 here:
中提出的Arry#difference
方法非常简单
arr << "Let's add a string that appears just once"
#=> ["footballs_jumba_10", "footballs_jumba_11", "footballs_jumba_12",
# "footballs_jumba_14", "alpha_romeo_11", "alpha_romeo_12",
# "alpha_juliet_10", "alpha_juliet_11", "Let's add a string that appears just once"]
a = arr.map { |s| s[0,10] }
#=> ["footballs_", "footballs_", "footballs_", "footballs_", "alpha_rome",
# "alpha_rome", "alpha_juli", "alpha_juli", "Let's add "]
b = a.difference(a.uniq)
#=> ["footballs_", "footballs_", "footballs_", "alpha_rome", "alpha_juli"]
b.uniq
#=> ["footballs_", "alpha_rome", "alpha_juli"]
你可以这样做:
def partial_duplicates(elements)
unique = {}
duplicates = {}
elements.each do |e|
partial = e[0..9]
# If the element is in the hash, it is a duplicate.
if first_element = unique[partial]
duplicates[first_element] = true
duplicates[e] = true
else
# include the element as unique
unique[partial] = e
end
end
duplicates.keys
end
这将 return 个独特的重复项。如果你想要所有重复项,你可以只使用 Array
.
此外,这个 return 是每个副本的所有完整表示,因为它看起来更有用,可能是您想要的:
partial_duplicates(arr)
=> ["footballs_jumba_10", "footballs_jumba_11", "footballs_jumba_12", "footballs_jumba_14", "alpha_romeo_11", "alpha_romeo_12", "alpha_juliet_10", "alpha_juliet_11"]
如果您只需要部分重复项,您可以将条件更改为:
if unique[partial]
duplicates[partial] = true
else
unique[partial] = true
end
然后:
partial_duplicates(arr)
=> ["footballs_", "alpha_rome", "alpha_juli"]