gpt4 book ai didi

python - 提高排序程序的效率;在终端中显示列排序完成百分比

转载 作者:行者123 更新时间:2023-12-01 04:39:55 28 4
gpt4 key购买 nike

我有一个大约 600 万行的大管道分隔输入文件,如下所示:

24|BBG000SJFVB0|EQ0000000009296012|OI SA-ADR|OIBR/C|US|ADR|Equity 16|BBG002PHVB83|EQ0000000022353186|BLOOM SELECT INCOME FUND|BLB-U|CT|Closed-End Fund|Equity
-50|BBG000V0TN75|EQ0000000010271114|MECHEL-PREF SPON ADR|MTL/P|US|ADR|Equity 20|BBG002S0ZR60|EQ0000000022739316|DIVIDEND 15 SPLIT CORP II-RT|DF-R|CT|Closed-End Fund|Equity
-20|BBG001R3LGM8|EQ0000000017879513|ING FLOATING RATE SENIOR LOA|ISL/U|CT|Closed-End Fund|Equity 0|BBG006M6SXL2|EQ0000000006846232|AA PLC|AA/|LN|Common Stock|Equity

Requirements are as below:
1. I need to sort this input file by 1st column and then 2nd column and then 2nd last column in that order
2. Displaying % of sort completion in terminal/console for e.g. "column 2 75% sort done"
3. finally output in a separate file.

我编写了下面的程序,该程序完美地按第一列排序。但如何结合所有其他条件呢?而且现在运行需要更多的时间。有没有更高效、更清洁的方法来做到这一点?唯一的问题是我们不能使用 CPAN 的任何额外的外部包。像使用 SED/AWK 这样的 Unix 解决方案都可以,但 Perl 更好。我刚刚知道内置的 Python 也在那里,所以这个解决方案也很受欢迎。

my (%link_strength);
{$data="datascope_input.txt";
$out="sort_file.txt";
open (my $indata , '<', $data)|| die "could not open $data :\n$!";
open (my $outdata , '>', $out)|| die "could not open $out :\n$!";
select $outdata;
my @array=(<$indata>);
for (@array){
$link_strength{$1}=$_ if /(?:[^|]+\|){0}([^|]+)/;
}
print $link_strength{$_} for (sort {$a<=>$b} keys %link_strength);
close ($outdata);
close ($indata);
}

最佳答案

正如我在评论中所说,Linux/Unix 系统排序可能会表现更好,但如果你真的想要 Perl,这会成功:

use strict;

sub main {
open F, 'input.txt' or die $!;
my @pairs;
while (<F>) {
my @fields = split(/\|/);
my $key = [ @fields[0, 1, -2] ];
push @pairs, [$key, $_];
}
close F;
my @sorted_pairs = sort {
my $a_key = $a->[0];
my $b_key = $b->[0];
$a_key->[0] <=> $b_key->[0]
|| $a_key->[1] cmp $b_key->[1]
|| $a_key->[2] cmp $b_key->[2]
} @pairs;
foreach my $pair (@sorted_pairs) {
print $pair->[1];
}
}

main;

正如我在评论中所说,我不知道如何内省(introspection)地收集进度信息。您可以通过计算发生了多少次比较来破解某些内容,但由于您永远无法确定最终的数字,因此无法计算完成百分比。

关于python - 提高排序程序的效率;在终端中显示列排序完成百分比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30950152/

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