删除重复的哈希值并在perl中保留它的计数

时间:2021-09-28 15:42:40

Lets say

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

I want to have two arrays:

我想要两个数组:

  1. This array should have unique key/value

    此数组应具有唯一键/值

    @array1=(key1 one key2 two key3 three)

    @ array1 =(key1 one key2 two key3 three)

  2. this array should have count of duplicates by value (eg here only three value are unique so 'one' is found twice and 'two' found twice again and 'three' found once).

    这个数组应该按值重复计数(例如,这里只有三个值是唯一的,因此'one'被发现两次,'two'被发现两次,'three'被发现一次)。

    @array2=(2 2 1)

    @ array2 =(2 2 1)

3 个解决方案

#1


I'm not sure why you want the final answer to be in that strange form (as opposed to a hash of unique key/values and a hash of value=>count mappings, but here you go...

我不确定为什么你希望最终的答案是以那种奇怪的形式出现(而不是哈希的唯一键/值和哈希值=>计数映射,但是你在这里......

You can also use %counts hash directly instead of pushing values into an array as you wanted above.

您也可以直接使用%count散列,而不是如上所述将值推送到数组中。

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"] ) ; 

Results:

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

#2


Ignoring the order issue ...

忽略订单问题......

#!/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
        ];

#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;

#1


I'm not sure why you want the final answer to be in that strange form (as opposed to a hash of unique key/values and a hash of value=>count mappings, but here you go...

我不确定为什么你希望最终的答案是以那种奇怪的形式出现(而不是哈希的唯一键/值和哈希值=>计数映射,但是你在这里......

You can also use %counts hash directly instead of pushing values into an array as you wanted above.

您也可以直接使用%count散列,而不是如上所述将值推送到数组中。

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"] ) ; 

Results:

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

#2


Ignoring the order issue ...

忽略订单问题......

#!/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
        ];

#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;