【问题标题】:Want to filter for the max result and print from a table that contains many results for multiple scenarios想要过滤最大结果并从包含多个场景的许多结果的表中打印
【发布时间】:2013-01-26 23:27:15
【问题描述】:

我有一个 CSV 表,其中包含 1024 个自变量25 个因变量 的合并数据。对于每个自变量(称为 1 .. 1024),我有 10 个不同的结果。我愿意

  1. 为每个自变量选择最佳结果,然后
  2. 将包含该信息的行通过管道传输到新的 CSV 文件中。

问 perl 似乎是一件相当容易的事情,也许用数组的数组的哈希来做很简单,但我仍然对如何为这个集合实现类似的东西感到困惑数据。

当前代码

我在打印匹配行时发现了 2009 年的 very helpful Q&A。经过一些修补后,它运行得相当好,但仍然存在一些问题:

  • 我必须对文件进行预排序,以便我的最大值是每种情况下出现的第一个值。
  • 我也错过了获得第一个自变量的最佳结果和
  • 在某些情况下,我收到多行返回给我,而不仅仅是最大值。

我相当肯定一定有更简单的方法来做到这一点,我将非常感谢任何对我(抄袭)脚本的帮助和/或建设性的批评。

谢谢!

这是我目前所拥有的:

#!/usr/bin/perl 
use warnings;
use strict;
unless ($#ARGV == 0) {
  print "USAGE:  get_best.pl  csvfile \n";
  exit;
}
### this is a script to get the best "score"
my $input = $ARGV[0];
my $outfile = "bestofthebest.csv";
if (-e $outfile ) {    
  system "rm $outfile";
}
open(my $fh,'<',"$input") || die "could not open $input"; #try to open input
open (SUMMARY, ">>","$outfile") || die "could not open $outfile"; #open output file for writing
my $this_line = "";
my $do_next = 0;

while (<$fh>) {
  chomp($_);
  my $last_line = $this_line;
  $this_line = $_;
  if ($this_line =~ m/Seq/) {
    print SUMMARY "$this_line\n";next;
  }
  my ($compound,     $rank,     $nnme,     $G1,     ..., $res1,     $res2,     $res3,     $res4,     $res5,     $res6    ) = split(/\s+/, $this_line, 26);
  my ($compound_old, $rank_old, $nnme_old, $G1_old, ..., $res1_old, $res2_old, $res3_old, $res4_old, $res5_old, $res6_old) = split(/\s+/, $last_line, 26);
  foreach ($compound == $compound_old) {
    if (($G1 >= $G1_old)){
      print SUMMARY "$this_line\n";
      print "\n $G1 G1 is >> $G1_old G1_old loop\n";
      print "\n compound is $compound G1 is $G1\n";
      $do_next = 1;
    }
    else {
      $last_line = "";
      $do_next = 0;
    } 
  }
}
close ($fh);
close (SUMMARY);

示例输入

这就是输入数据的样子(很明显,我省略了一些列和行)

10  8   3   -18.08  -1.4    -16.68  -15.94  -2.13   -9.45
11  10  4   -15.2   3.2 -18.4   -18.02  2.82    -5
11  5   4   -15.22  2.71    -17.92  -15.88  0.66    -4.51
11  7   4   -14.06  3.84    -17.89  -16.7   2.64    -5.73
11  4   4   -16.63  0.48    -17.1   -15.75  -0.87   -5.92
11  6   4   -15.21  1.83    -17.04  -18.41  3.21    -7
11  9   4   -15.18  1.82    -17 -16.56  1.38    -7.09
11  8   4   -14.98  1.93    -16.91  -16.78  1.79    -10.81
11  2   4   -18.75  -1.95   -16.8   -17.83  -0.92   -7.35
11  1   4   -19.67  -3.17   -16.5   -16.4   -3.27   -9.01
11  3   4   -16.69  -0.54   -16.14  -16.35  -0.34   -9.17
12  7   4   -19.54  -1.14   -18.41  -17.74  -1.81   -2.79
12  9   4   -19.09  -1.01   -18.08  -16.01  -3.09   -5.56
12  4   4   -19.48  -2.18   -17.3   -16.34  -3.14   -4
12  2   4   -19.86  -2.77   -17.1   -15.97  -3.9    -2.96
12  8   4   -19.49  -2.45   -17.03  -16.39  -3.1    -7.19
12  1   4   -20.28  -3.33   -16.95  -17.12  -3.16   -5.18
12  3   4   -18.78  -1.93   -16.86  -17.81  -0.98   -5.39
12  5   4   -19.63  -2.86   -16.77  -16.41  -3.22   -6.54
12  6   4   -19.81  -3.25   -16.56  -16.53  -3.27   -7.19
12  10  4   -19.39  -2.95   -16.44  -17.42  -1.97   -7.67
13  1   3   -13.05  6.35    -19.4   -18.71  5.66    -6.43
13  8   3   -21.44  -2.32   -19.11  -17.08  -4.36   -1.93
13  3   3   -16 2.94    -18.94  -19.24  3.24    -2.78
13  2   3   -13.79  4.9 -18.7   -17.35  3.56    -4.72
13  6   3   -22.08  -3.4    -18.68  -20.12  -1.96   -6.74
13  9   3   -18.98  -0.32   -18.66  -15.97  -3.01   -3.06
13  7   3   -20.4   -2.08   -18.32  -18.24  -2.17   -5.71
13  5   3   -19.94  -1.62   -18.32  -19.42  -0.52   -7.44
13  10  3   -19.26  -1.25   -18.01  -17.52  -1.74   -5.68
13  4   3   -17.75  -1.33   -16.42  -17.75  0   -9.15
14  9   3   -22.23  -3.43   -18.79  -16.68  -5.55   -3.91
14  5   3   -21.32  -2.95   -18.37  -18.08  -3.24   -6.03
14  7   3   -24.25  -6.29   -17.96  -18.78  -5.47   -9.21
14  6   3   -21.03  -3.14   -17.89  -19.17  -1.86   -10.11
14  4   3   -21.59  -3.93   -17.67  -19.32  -2.28   -6.55
14  1   3   -22.43  -4.79   -17.63  -18.09  -4.34   -5.63

电流输出:

10  2   3   -10.11  8.94    -19.04  -18.48  8.38    -4.09
11  5   4   -15.22  2.71    -17.92  -15.88  0.66    -4.51
12  7   4   -19.54  -1.14   -18.41  -17.74  -1.81   -2.79
12  6   4   -19.81  -3.25   -16.56  -16.53  -3.27   -7.19
13  8   3   -21.44  -2.32   -19.11  -17.08  -4.36   -1.93
14  9   3   -22.23  -3.43   -18.79  -16.68  -5.55   -3.91
15  10  4   -21.51  -1.51   -20 -17.63  -3.88   -2.45
16  5   4   -17.81  2.56    -20.37  -19.09  1.28    -1.19
16  2   4   -16.61  1.97    -18.58  -21.06  4.45    -6.47

【问题讨论】:

    标签: perl


    【解决方案1】:

    也许以下内容会有所帮助:

    use strict;
    use warnings;
    
    my %hash;
    
    while (<DATA>) {
        my ( $indVarID, $val ) = (split)[ 0, 3 ];
    
        $hash{$indVarID} = [ $val, $_ ]
          if !exists $hash{$indVarID}
          or $hash{$indVarID}[0] < $val;
    }
    
    print $hash{$_}[1] for sort { $a <=> $b } keys %hash;
    
    __DATA__
    11  7   4   -14.06  3.84    -17.89  -16.7   2.64    -5.73
    11  4   4   -16.63  0.48    -17.1   -15.75  -0.87   -5.92
    11  6   4   -15.21  1.83    -17.04  -18.41  3.21    -7
    11  9   4   -15.18  1.82    -17 -16.56  1.38    -7.09
    11  8   4   -14.98  1.93    -16.91  -16.78  1.79    -10.81
    11  2   4   -18.75  -1.95   -16.8   -17.83  -0.92   -7.35
    11  1   4   -19.67  -3.17   -16.5   -16.4   -3.27   -9.01
    11  3   4   -16.69  -0.54   -16.14  -16.35  -0.34   -9.17
    12  7   4   -19.54  -1.14   -18.41  -17.74  -1.81   -2.79
    12  9   4   -19.09  -1.01   -18.08  -16.01  -3.09   -5.56
    12  4   4   -19.48  -2.18   -17.3   -16.34  -3.14   -4
    12  2   4   -19.86  -2.77   -17.1   -15.97  -3.9    -2.96
    12  8   4   -19.49  -2.45   -17.03  -16.39  -3.1    -7.19
    12  1   4   -20.28  -3.33   -16.95  -17.12  -3.16   -5.18
    12  3   4   -18.78  -1.93   -16.86  -17.81  -0.98   -5.39
    12  5   4   -19.63  -2.86   -16.77  -16.41  -3.22   -6.54
    12  6   4   -19.81  -3.25   -16.56  -16.53  -3.27   -7.19
    12  10  4   -19.39  -2.95   -16.44  -17.42  -1.97   -7.67
    13  1   3   -13.05  6.35    -19.4   -18.71  5.66    -6.43
    13  8   3   -21.44  -2.32   -19.11  -17.08  -4.36   -1.93
    13  3   3   -16 2.94    -18.94  -19.24  3.24    -2.78
    13  2   3   -13.79  4.9 -18.7   -17.35  3.56    -4.72
    13  6   3   -22.08  -3.4    -18.68  -20.12  -1.96   -6.74
    13  9   3   -18.98  -0.32   -18.66  -15.97  -3.01   -3.06
    13  7   3   -20.4   -2.08   -18.32  -18.24  -2.17   -5.71
    13  5   3   -19.94  -1.62   -18.32  -19.42  -0.52   -7.44
    13  10  3   -19.26  -1.25   -18.01  -17.52  -1.74   -5.68
    13  4   3   -17.75  -1.33   -16.42  -17.75  0   -9.15
    14  9   3   -22.23  -3.43   -18.79  -16.68  -5.55   -3.91
    14  5   3   -21.32  -2.95   -18.37  -18.08  -3.24   -6.03
    14  7   3   -24.25  -6.29   -17.96  -18.78  -5.47   -9.21
    14  6   3   -21.03  -3.14   -17.89  -19.17  -1.86   -10.11
    14  4   3   -21.59  -3.93   -17.67  -19.32  -2.28   -6.55
    14  1   3   -22.43  -4.79   -17.63  -18.09  -4.34   -5.63
    

    输出:

    11  7   4   -14.06  3.84    -17.89  -16.7   2.64    -5.73
    12  3   4   -18.78  -1.93   -16.86  -17.81  -0.98   -5.39
    13  1   3   -13.05  6.35    -19.4   -18.71  5.66    -6.43
    14  6   3   -21.03  -3.14   -17.89  -19.17  -1.86   -10.11
    

    这会构建一个数组散列 (HoA),其中键是自变量 ID,值是对二元素列表的引用。列表中的第零个元素是在记录的第四列中找到的值。第一个元素是记录。

    在读取记录时,如果自变量的新值大于旧值(或者如果没有旧值),则新值和记录将存储在列表中。

    完成后,对键进行数字排序,并打印包含每个自变量 ID 的最大值的记录。

    【讨论】:

    • 绝对有帮助!它有效,它更短,并且是更正确地使用哈希学习的完美方式——非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-14
    相关资源
    最近更新 更多