如何找到与最大计算值相关的键?

发布于 2024-12-15 08:05:15 字数 555 浏览 0 评论 0原文

我试图找出哈希中的最大值以及该最大值的对应键。我的哈希看起来像

%hash = (
    bob => "4.9",
    gita => "3.9 , 6.8",
    diu => "3.0",
);

现在我想找到该哈希中的最大值及其所属的键。

所需的输出是

gita 6.8  

我试图按升序对 %hash 中的值进行排序以获得最大值,如下所示

sub hashValueAscendingNum {
    $hash{$a} cmp $hash{$b};
}

foreach my $highest (sort hashValueAscendingNum(keys(%hash))) {
    print "\t $hash{$highestMagnitude} \t\t $highest \n";
}

我希望检查哈希中的所有值,并且具有最大值的值应该是带着钥匙回来了。

我怎样才能做到这一点?

I am trying to find out maximum value in a hash and corresponding key to that maximum value. My hash looks like

%hash = (
    bob => "4.9",
    gita => "3.9 , 6.8",
    diu => "3.0",
);

Now I want to find the maximum value in that hash with the key it belongs.

Output needed is

gita 6.8  

I am trying to sort the values in %hash in ascending order to get the maximum value like this

sub hashValueAscendingNum {
    $hash{$a} cmp $hash{$b};
}

foreach my $highest (sort hashValueAscendingNum(keys(%hash))) {
    print "\t $hash{$highestMagnitude} \t\t $highest \n";
}

I want all the values in the hash to be checked, and the one with maximum value should be returned with its key.

How can i do that?

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

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

发布评论

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

评论(4

苏大泽ㄣ 2024-12-22 08:05:15

您需要首先将每个键与原始 %hash 中对应的最高值关联起来,然后找到与最高值关联的键。

#!/usr/bin/env perl

use strict; use warnings;

use List::Util qw( max );

my  %hash = (
    bob => [ 4.9 ],
    gita => [ 3.9, 6.8 ],
    diu => [ 3.0 ],
);

my %max = map { $_ => max @{ $hash{$_} } } keys %hash;

my ($argmax) = (sort { $max{$b} <=> $max{$a} } keys %max)[0];
my $max = $max{ $argmax };

print join(' => ', $argmax, $max), "\n";

当然,这是非常低效的(尤其是使用 sort),但对于您显示的尺寸,这并不重要。为了完整起见,以下是使用 each 的更高效版本:

#!/usr/bin/env perl

use strict; use warnings;

use List::Util qw( max );

my  %hash = (
    bob => [ 4.9 ],
    gita => [ 3.9, 6.8 ],
    diu => [ 3.0 ],
);

my ($argmax, $max) = @{ init_argmax_max(\%hash) };

while (my ($k, $v) = each %hash) {
    $v = max @{ $v };
    if ( $v > $max ) {
        $argmax = $k;
        $max = $v;
    }
}

print join(' => ', $argmax, $max), "\n";

sub init_argmax_max {
    my ($hash) = @_;
    my ($argmax, $max) = each %{ $hash };

    keys %{ $hash };

    $max = max @{ $max };

    return [$argmax, $max];
}

You need to first associate each key with the highest of the values corresponding to it in the original %hash and then find the key associated with the highest value.

#!/usr/bin/env perl

use strict; use warnings;

use List::Util qw( max );

my  %hash = (
    bob => [ 4.9 ],
    gita => [ 3.9, 6.8 ],
    diu => [ 3.0 ],
);

my %max = map { $_ => max @{ $hash{$_} } } keys %hash;

my ($argmax) = (sort { $max{$b} <=> $max{$a} } keys %max)[0];
my $max = $max{ $argmax };

print join(' => ', $argmax, $max), "\n";

Of course, this is very inefficient (esp. using sort) but for the dimensions you showed, it does not matter. For completeness, here is a more efficient version using each:

#!/usr/bin/env perl

use strict; use warnings;

use List::Util qw( max );

my  %hash = (
    bob => [ 4.9 ],
    gita => [ 3.9, 6.8 ],
    diu => [ 3.0 ],
);

my ($argmax, $max) = @{ init_argmax_max(\%hash) };

while (my ($k, $v) = each %hash) {
    $v = max @{ $v };
    if ( $v > $max ) {
        $argmax = $k;
        $max = $v;
    }
}

print join(' => ', $argmax, $max), "\n";

sub init_argmax_max {
    my ($hash) = @_;
    my ($argmax, $max) = each %{ $hash };

    keys %{ $hash };

    $max = max @{ $max };

    return [$argmax, $max];
}
美人迟暮 2024-12-22 08:05:15

哈希只有一个键和一个值,并且每个键必须是唯一的。在您最初的问题中,您有这样的问题:

%hash = (
   bob => "4.9",
   gita =>"3.9 , 6,8",
   diu => "3.0",
);

嗯,gita 不能有两个值。散列中也不可能有两个等于 gita 的键。因此,您不能使用简单的哈希来存储您的值。

不过,有一些方法可以通过使用引用来解决这个问题。例如,散列中的每个元素都可以包含对数组的引用。因此,您的数据结构可以如下所示:

%hash = (
    bob =>  [(4.9)],
    gita => [(3.9, 6.8)],
    diu  => [(3.0)],
);

[] 标记对数组的引用。

但是,这并不能真正解决您的特定问题,因为您现在必须遍历哈希中的每个键,然后遍历每个键的数组中的每个元素,并对它们进行排序。您可以创建一个排序子例程,但仅仅因为您可以说 sort 并不能提高它的效率。

也许您需要的是数组的数组。这将解决 gita 有两个值的问题,但使排序更容易一些。想象一下这样的结构:

my @array = (
    [bob  => 4.9],
    [gita => 3.9],
    [gita => 6.8],
    [diu  => 3.0],
);

现在,我们可以根据 $array[$x]->[1] 的值对 @array 进行排序!对于数组 @array 的每个元素,我们所需要做的就是将 $a->[1] 与 $b->[1] 进行比较。然后,如果我们对其进行反向排序,最大的元素将是 $array[0]。名称为 $array[0]->[0],元素为 $array->[0]->[1]

#! /usr/bin/env perl
use strict;
use warnings;
use feature qw(say switch);

my @array = (
    [bob  => 4.9],
    [gita => 3.9],
    [gita => 6.8],
    [diu  => 3.0],
);

@array = reverse sort mysort @array;

say "$array[0]->[0] $array[0]->[1]";

sub mysort {
    $a->[1] <=> $b->[1];
}

输出是:

gita 6.8.

您注意到 Perldoc 的 perllol 的链接了吗?如果您以前从未使用过 Perl 参考资料,我建议您阅读它。

A hash has only a single key and a single value, and each key must be unique. In your original problem you have this:

%hash = (
   bob => "4.9",
   gita =>"3.9 , 6,8",
   diu => "3.0",
);

Well, gita can't have two values. Nor, can you have two keys in your hash equal to gita. Thus, you can't use a simple hash to store your values.

There are ways around this though by using references. For example, each element in your hash can contain a reference to an array. Thus, your data structure can look like this:

%hash = (
    bob =>  [(4.9)],
    gita => [(3.9, 6.8)],
    diu  => [(3.0)],
);

The [ and ] marks a reference to an array.

However, this wouldn't really solve your particular problem since you now have to go through each key in the hash, then each element in the array for each key, and sort those. You could create a sorting subroutine, but just because you can say sort doesn't make it efficient.

Maybe what you need is an array of arrays. This will get rid of the issue you have with gita having two values, but make sorting a bit easier. Imagine a structure like this:

my @array = (
    [bob  => 4.9],
    [gita => 3.9],
    [gita => 6.8],
    [diu  => 3.0],
);

Now, we can do a sort on @array depending upon the value of $array[$x]->[1]! All we need is for each element of the array @array is to compare $a->[1] with $b->[1]. Then, if we do a reverse sort on it, the biggest element will be $array[0]. The name is $array[0]->[0] and the element is $array->[0]->[1].

#! /usr/bin/env perl
use strict;
use warnings;
use feature qw(say switch);

my @array = (
    [bob  => 4.9],
    [gita => 3.9],
    [gita => 6.8],
    [diu  => 3.0],
);

@array = reverse sort mysort @array;

say "$array[0]->[0] $array[0]->[1]";

sub mysort {
    $a->[1] <=> $b->[1];
}

And the output is:

gita 6.8.

You notice that link to Perldoc's perllol? I suggest you read it if you've never worked with Perl references before.

心房的律动 2024-12-22 08:05:15
#!/usr/bin/perl
use warnings;
use strict;

my %hash = (
    bob  => [ 4.9 ],
    gita => [ 3.9, 6.8 ],
    diu  => [ 3.0 ],
);

my $max_key;
my $max_val=0;

foreach my $key (keys %hash) {
    foreach my $val ( @{$hash{$key}} ) {
        ($max_key, $max_val) = ($key, $val)
            if $val > $max_val;
    }
}

print "$max_key => $max_val\n";
#!/usr/bin/perl
use warnings;
use strict;

my %hash = (
    bob  => [ 4.9 ],
    gita => [ 3.9, 6.8 ],
    diu  => [ 3.0 ],
);

my $max_key;
my $max_val=0;

foreach my $key (keys %hash) {
    foreach my $val ( @{$hash{$key}} ) {
        ($max_key, $max_val) = ($key, $val)
            if $val > $max_val;
    }
}

print "$max_key => $max_val\n";
倾`听者〃 2024-12-22 08:05:15

你得到了几个很好的答案。现在是一个坏的(假设你修复了哈希中的小数分隔符):

my %hash = (bob => "4.9",
            gita =>"3.9 , 6.8",
            diu => "3.0",
            );

my $max = (map{join" ",@$_[0,1]}sort{$b->[1]-$a->[1]}map{[$_,sort{$b-$a}split(/ , /,$hash{$_})]}keys%hash)[0];

print "$max\n";

输出:

gita 6.8

永远不要这样做,除非你故意试图打高尔夫球和/或混淆它。

You got several good answers. Now a bad one (assuming you fix the decimal separator in the hash):

my %hash = (bob => "4.9",
            gita =>"3.9 , 6.8",
            diu => "3.0",
            );

my $max = (map{join" ",@$_[0,1]}sort{$b->[1]-$a->[1]}map{[$_,sort{$b-$a}split(/ , /,$hash{$_})]}keys%hash)[0];

print "$max\n";

Output:

gita 6.8

Never do this unless you're deliberately trying to golf and/or obfuscate it though.

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