如何按值的频率排序?

发布于 2024-10-09 02:37:19 字数 544 浏览 0 评论 0 原文

我正在尝试创建一个程序来计算数据文件列中出现的不同值。所以,如果一列的可能值是 A、B、C,那么输出就像

A   456
B   234
C   344

我已经能够通过执行类似的操作轻松获得 A、B 和 C 的运行计数

my %count; 
for my $f (@ffile) {

    open F, $f || die "Cannot open $f: $!";

    while (<F>) {
       chomp;
       my @U = split / /;

       $count{$U[2]}++; 
    }

}
foreach my $w (sort keys %count) {
    printf $w\t$count{$w};
}

例如这里我正在计算给定路径中文件的第二列。

如何按计数而不是键(或值 A、B、C)对 printf 的输出进行排序以获得以下输出?

A   456
C   344
B   234

I am trying to create a program to count the different values that occur in a column of a data file. So, it would be something like, if the possible values of a column are A, B, C. The output is something like

A   456
B   234
C   344

I have been able to get the running counts of A, B and C easily by doing something like this

my %count; 
for my $f (@ffile) {

    open F, $f || die "Cannot open $f: $!";

    while (<F>) {
       chomp;
       my @U = split / /;

       $count{$U[2]}++; 
    }

}
foreach my $w (sort keys %count) {
    printf $w\t$count{$w};
}

For instance here I am counting the second column of the file in the path given.

How do I sort the output of the printf by the counts rather than the keys (or values A, B, C) to get the following output?

A   456
C   344
B   234

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

世界等同你 2024-10-16 02:37:19

这是常见问题解答:

perldoc -q 排序

use warnings;
use strict;

my %count = (
    A => 456,
    B => 234,
    C => 344
);

for my $w (sort { $count{$b} <=> $count{$a} } keys %count) {
    print "$w\t$count{$w}\n";
}

输出:

A       456
C       344
B       234

This is a FAQ:

perldoc -q sort

use warnings;
use strict;

my %count = (
    A => 456,
    B => 234,
    C => 344
);

for my $w (sort { $count{$b} <=> $count{$a} } keys %count) {
    print "$w\t$count{$w}\n";
}

Output:

A       456
C       344
B       234
不离久伴 2024-10-16 02:37:19
for my $w (sort {$count{$b} <=> $count{$a}} keys %count) {
    print "$w\t$count{$w}\n";
}
for my $w (sort {$count{$b} <=> $count{$a}} keys %count) {
    print "$w\t$count{$w}\n";
}
坏尐絯℡ 2024-10-16 02:37:19

一些补充意见:

输出类似于...通过执行类似的操作

,如果您粘贴实际代码(尽可能缩写),您将帮助我们帮助您。
当人们重新创建实际代码时,他们经常掩盖或忽略问题的根源。

   chomp;
   my @U = split / /;

这会分割空格字符并查找第二个空格之后的计数;它通常更容易做到:

   my @U = split ' ';

split 与常量空格一起使用,而不是使用正则表达式在任何空格序列上进行拆分,就像 split /\s+/ 一样,只不过它忽略尾随空格。 ..这是一件很常见的事情,因此有一种特殊的语法。请注意,咀嚼变得不必要了。

Some additional comments:

The output is something like...by doing something like this

You help us help you if you paste your actual code, abbreviated where possible.
When people recreate their actual code, they often obscure or omit the very source of their problem.

   chomp;
   my @U = split / /;

This splits on space characters and looks for the count after the second space; it's often easier to do:

   my @U = split ' ';

split used with a constant space instead of a regex splits on any sequence of whitespace, like split /\s+/ except that it ignores trailing whitespace...this is a common enough thing to do that there is this special syntax for it. Note that the chomp becomes unnecessary.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文