Ruby 如何计算数组中相同的元素.

mehdi-farsi · 2020年09月07日 · 993 次阅读
a = %w[one two two three three three]

# Ruby 2.4+
a.group_by(&:itself).transform_values(&:count) # => {"one" => 1, "two" => 2, "three" => 3}

# Ruby 2.7+
a.tally # => {"one" => 1, "two" => 2, "three" => 3}

暂无回复。
需要 登录 后方可回复, 如果你还没有账号请 注册新账号