Ruby 初心者スレッド Part 67 (748レス)
Ruby 初心者スレッド Part 67 http://mevius.5ch.net/test/read.cgi/tech/1608837867/
上
下
前次
1-
新
通常表示
512バイト分割
レス栞
抽出解除
必死チェッカー(本家)
(べ)
自ID
レス栞
あぼーん
394: デフォルトの名無しさん (ワッチョイ 6b2c-bfGZ) [sage] 2022/03/08(火) 08:30:39.25 ID:uRRrzdTb0 配列内の要素の出現回数を求める ary = [ 1, "a", 2, "b", "a", 1, "a" ] p results = ary.each_with_object( Hash.new( 0 ) ){ |elem, hash| hash[ elem ] += 1 } #=> { 1=>2, "a"=>3, 2=>1, "b"=>1 } Ruby には、下のPython のcollections.Counter みたいな関数がありますか? import collections l = ['a', 'a', 'a', 'a', 'b', 'c', 'c'] c = collections.Counter(l) print(c) #=> Counter({'a': 4, 'c': 2, 'b': 1}) Rubyで、誰かが作ったものはあるけど https://gist.github.com/cielavenir/501c0cf491e10d905d4307bdeb2596ea http://mevius.5ch.net/test/read.cgi/tech/1608837867/394
メモ帳
(0/65535文字)
上
下
前次
1-
新
書
関
写
板
覧
索
設
栞
歴
スレ情報
赤レス抽出
画像レス抽出
歴の未読スレ
AAサムネイル
Google検索
Wikipedia
ぬこの手
ぬこTOP
0.033s