Python中是否有类似于Counter的内置方法?
Is there a builtin method similar to Counter in Python?
我已经翻阅了文档,但似乎找不到与 Python.
中的 Counter 类似的内容
我知道我可以简单地编写类似的东西,但是内置函数会很方便。
简单的例子:
my %h; %h{$_}++ for @test;
Bag class 为所欲为。
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
我已经翻阅了文档,但似乎找不到与 Python.
中的 Counter 类似的内容我知道我可以简单地编写类似的东西,但是内置函数会很方便。
简单的例子:
my %h; %h{$_}++ for @test;
Bag class 为所欲为。
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