我已经浏览过文档,但是似乎找不到与Python中的Counter类似的东西。
我知道我可以简单地编写类似的东西,但是内置将非常方便。
琐碎的例子:
my %h; %h{$_}++ for @test;
最佳答案
Bag类可以满足您的需求。
my %h is Bag = @test;
或者,如果您只想胁迫:
my $bag = @test.Bag;
无论哪种情况,都可以像使用任何普通的
Hash
一样使用该对象。# show sorted with most frequent first
say "{.key} seen {.value} times" for %h.sort: -*.value
关于raku - 有没有类似于Python中的Counter的内置方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51484197/