gpt4 book ai didi

linux - 为什么我的哈希值(来自 csv)的大小恰好是行数值的一半?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:43:28 26 4
gpt4 key购买 nike

我编写了一个简单的 perl 脚本来计算散列中值和/或键的数量,散列是根据 csv 的内容创建的。 csv 看起来像这样:

311552047969,THE UPSETTERS   RETURN OF THE SUPER APE VINYL LP 1978 ,http://www.ebay.co.uk/itm/UPSETTERS-RETURN-SUPER-APE-VINYL-LP-1978-/311552047969,56.0                  
322016291276,Queen A Kind Of Magic NZ Orange Vinyl,http://www.ebay.co.uk/itm/Queen-Kind-Magic-NZ-Orange-Vinyl-/322016291276,165.0
252288285264,Goldfrapp Black cherry vinyl record lp,http://www.ebay.co.uk/itm/Goldfrapp-Black-cherry-vinyl-record-lp-/252288285264,70.0
331782523967,Reggae vinyl johny pram pram ,http://www.ebay.co.uk/itm/Reggae-vinyl-johny-pram-pram-/331782523967,73.0
391392294381,Various vinyl albums,http://www.ebay.co.uk/itm/Various-vinyl-albums-/391392294381,102.24

这是我计算行数的脚本。

#!/bin/perl


open CSV2, "<csv2" or die;
@csv2=<CSV2>;
close CSV2;


%hash = @csv2;

@keys = keys %hash;
@values = values %hash;

$size = @values;
print "Hash size is $size";

csv 中的实际行数是 6374,但我的代码的输出正好是它的一半 - 3187。

我确定对此有一个简单的解释,但为什么散列的大小(即值/键的数量)与我的 csv 中的行数不匹配?

最佳答案

当您将列表分配给散列时,列表的偶数索引元素成为键,列表的奇数索引元素成为值。因此,如果列表中的元素数量为偶数,哈希中的键数将是列表中元素数量的一半,哈希中值数量将是列表中元素数量的一半。否则,散列中将有 n/2 + 1 键和 n 值,其中 n 是列表中的元素数。

您可能想要做的是将元素插入到由 CSV 的第 0 列中的 ID 键控的散列中。

#!/usr/bin/env perl

use strict;
use warnings;

use Text::CSV_XS;

my @fields = qw( title url price);
my $csv = Text::CSV_XS->new({ allow_whitespace => 1});
my %data;

while (my $row = $csv->getline(\*DATA)) {
my ($key) = shift @$row;
next unless defined($key) and length($key);
for ( @$row ) {
s/^\s+//;
s/\s+\z//;
}
@{ $data{$key} }{ @fields } = @$row;
}

use Data::Dumper;
print Dumper \%data;

__DATA__
311552047969,THE UPSETTERS RETURN OF THE SUPER APE VINYL LP 1978 ,http://www.ebay.co.uk/itm/UPSETTERS-RETURN-SUPER-APE-VINYL-LP-1978-/311552047969,56.0
322016291276,Queen A Kind Of Magic NZ Orange Vinyl,http://www.ebay.co.uk/itm/Queen-Kind-Magic-NZ-Orange-Vinyl-/322016291276,165.0
252288285264,Goldfrapp Black cherry vinyl record lp,http://www.ebay.co.uk/itm/Goldfrapp-Black-cherry-vinyl-record-lp-/252288285264,70.0
331782523967,Reggae vinyl johny pram pram ,http://www.ebay.co.uk/itm/Reggae-vinyl-johny-pram-pram-/331782523967,73.0
391392294381,Various vinyl albums,http://www.ebay.co.uk/itm/Various-vinyl-albums-/391392294381,102.24

输出

$ ./pppp.pl$VAR1 = {          '252288285264' => {                              'url' => 'http://www.ebay.co.uk/itm/Goldfrapp-Black-cherry-vinyl-record-lp-/252288285264',                              'title' => 'Goldfrapp Black cherry vinyl record lp',                              'price' => '70.0'                            },          '322016291276' => {                              'price' => '165.0',                              'title' => 'Queen A Kind Of Magic NZ Orange Vinyl',                              'url' => 'http://www.ebay.co.uk/itm/Queen-Kind-Magic-NZ-Orange-Vinyl-/322016291276'                            },          '311552047969' => {                              'url' => 'http://www.ebay.co.uk/itm/UPSETTERS-RETURN-SUPER-APE-VINYL-LP-1978-/311552047969',                              'title' => 'THE UPSETTERS   RETURN OF THE SUPER APE VINYL LP 1978',                              'price' => '56.0'                            },          '331782523967' => {                              'url' => 'http://www.ebay.co.uk/itm/Reggae-vinyl-johny-pram-pram-/331782523967',                              'price' => '73.0',                              'title' => 'Reggae vinyl johny pram pram'                            },          '391392294381' => {                              'url' => 'http://www.ebay.co.uk/itm/Various-vinyl-albums-/391392294381',                              'title' => 'Various vinyl albums',                              'price' => '102.24'                            }        };

关于linux - 为什么我的哈希值(来自 csv)的大小恰好是行数值的一半?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38903667/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com