【问题标题】:removing duplicate hash value and keeping a count of it in perl删除重复的哈希值并在 perl 中保留它的计数
【发布时间】:2010-11-01 12:55:25
【问题描述】:

让我们说

%hash = (
        key1 => 'one',
        key2 => 'two',
        key3 => 'three',
        key4 => 'two',
        key5 => 'one',
);

我想要两个数组:

  1. 这个数组应该有唯一的键/值

    @array1=(key1 一键2 二键3 三)

  2. 这个数组应该按值计算重复项(例如,这里只有三个值是唯一的,所以“一”被找到两次,“二”又被找到两次,“三”被找到一次)。

    @array2=(2 2 1)

【问题讨论】:

  • “唯一键/值”是否意味着键或值必须唯一,或者键和值的组合必须唯一,或者仅键必须唯一?
  • 你假设@array2 的顺序是什么。换句话说,为什么“一”的计数在“二”的计数之前?
  • 什么决定了保留哪些密钥?不要说 order,因为不同 perl 中返回键的顺序不一样,也和源文件中给出的顺序不一样。
  • @array2 的顺序应该类似于 value1 value2 因为我想从不同的键中删除相同的元素。 @array1 的顺序应该与 @array2 匹配以跟踪哪些键/值相同以及多少次?

标签: perl hash


【解决方案1】:

忽略订单问题...

#!/usr/bin/perl

use strict;
use warnings;

my %hash = qw( key1 one key2 two key3 three key4 two key5 one );

my %counts;
$counts{ $_ }++ for values %hash;

my %uniq = reverse %hash;

my (@array1, @array2);

while ( my ($k, $v) = each %uniq ) {
    push @array1, $v, $k;
    push @array2, $counts{$k};
}

use Data::Dumper;
print Dumper \@array1, \@array2;


__END__

C:\Temp> t.pl
$VAR1 = [
          'key3',
          'three',
          'key5',
          'one',
          'key2',
          'two'
        ];
$VAR2 = [
          1,
          2,
          2
        ];

【讨论】:

  • 是的。即使这个也能完美解决我的问题。谢谢。谢谢大家。这是我的第一篇文章,没想到会有这么快的反应和做同一件事的不同方式。
【解决方案2】:

我不确定您为什么希望最终答案采用那种奇怪的形式(而不是唯一键/值的散列和 值的散列=>计数映射,但在这里你走吧……

您也可以直接使用 %counts 哈希,而不是像上面想要的那样将值推送到数组中。

use strict; 
use Data::Dumper;

my %hash = ( key1 => "one", key2 => "two", key3 => "three", key4 => "two", key5 => "one" );
my %counts = ();
my @counts = ();
my %unique = ();
foreach my $key (sort keys %hash) {
    my $value = $hash{$key}; 
    if (not exists $counts{$value}) {
        $unique{$key} = $value;
    }
    $counts{$value}++;
};
my @unique_keys = sort keys %unique; # Fix the sorting to your desired one 
                                     # if default is not what you meant

# You can also use %counts hash directly 
#instead of pushing values into an array as you wanted above.
foreach my $key (@unique_keys) {
    push @counts, $counts{ $unique{$key} }
};

# Print
print Data::Dumper->Dump([\@unique_keys, \@counts, \%unique],  
                         ["unique_keys", "counts", "unique"] ) ; 

结果:

$unique_keys = [
                 'key1',
                 'key2',
                 'key3'
               ];
$counts = [
            2,
            2,
            1
          ];
$unique = {
            'key2' => 'two',
            'key1' => 'one',
            'key3' => 'three'
          };

【讨论】:

  • 我对 perl 还是很陌生 .... 是的,我同意你的观点,也可以使用唯一的键/值进行散列。我实际上想要记录所有值及其各自的键值。我只是不知道如何使用哈希来做到这一点......非常感谢......我得到了我想要的......
【解决方案3】:
my %hash = (
        key1 => 'one',
        key2 => 'two',
        key3 => 'three',
        key4 => 'two',
        key5 => 'one',
);

my %counts;

$counts{$_}++ foreach values %hash;

my @elements = keys %hashes;

my @frequency = values %hashes;

【讨论】:

    猜你喜欢
    • 2013-11-09
    • 1970-01-01
    • 2015-05-10
    • 1970-01-01
    • 2019-09-16
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    • 2015-08-01
    相关资源
    最近更新 更多