Perl和Python,一个实用的并排示例。 [英] Perl and Python, a practical side-by-side example.

查看:71
本文介绍了Perl和Python,一个实用的并排示例。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Python的新手并且在Perl方面经验丰富,虽然

的体验仅限于我每天使用的东西。


我写的Perl和Python中的相同脚本,输出是相同的
。运行速度类似(非常快),行数是

类似。


现在他们都在工作,我看着代码和想知道

对代码的Perl特定和Python特定的改进将会是什么样的,b / b
看起来像是由其他人更了解的个人判断

语言。


我不是在寻找最少的行数或其他任何东西

会使代码在六个月内难以阅读。只是

任何我无法正常做事或以坏

方式做事的情况。


我'我附加了Perl和Python版本,我愿意接受

评论。该脚本从标准输入读取文件,

为每个唯一ID(piid)找到最佳记录。最佳定义

如下:记录的最新到期日期(字段5)

状态(字段1)与所需状态匹配(字段6) 。如果

没有与所需状态相匹配的记录,那么只需花费

最新到期日期。


感谢您购买是时候看看这些了。<​​br />

Shawn


################### ################################################################# #####

Perl代码:

########################## ################################################# />
#! / usr / bin / env perl


使用警告;

使用严格;


我的$ piid;

我的$ row;

我的%输入;

我的$ best;

我的$ curr;


foreach $ row(<>){


chomp($ row);

$ piid =(拆分(/ \t /,$ row))[0];


push(@ {$ input {$ piid}},$ row);

$

for $ piid(密钥(%输入)){


$ best ="" ;;
< $ $
for $ curr(@ {$ input {$ piid}}){

if($ best eq""){

$最好= $ curr;

}否则{

#如果当前记录是正确状态


if((split( / \t /,$ curr))[1] eq(split(/ \t /,$ curr))[6]){

#如果现有记录是正确的状态

if((split(/ \t /,$ best))[1] eq(split(/ \t /,$ curr))[6]){

if((split(/ \t /,$ curr))[5] gt(split(/ \t /,$ best))[5]){

$ best = $ curr;

}

}否则{

$ best = $ curr;

}

}否则{

#如果现有记录没有正确的状态

#和新的有一个更新的到期日期

if(((split(/ \t /,$ best))[1] ne(split(/ \t /) ,$ curr))[6])和

((拆分(/ \t /,$ curr))[5] gt(拆分(/ \t /,$ best))[ 5])){

$ best = $ curr;

}

}

}

}

打印" $ best\\\
";

}


###### ################################################## ##################

结束Perl代码

############# ############################################################# ###########


####################### ################################################## #

Python代码

############################### ###########################################

#! / usr / bin / env python

导入系统



recs = {}


输入行:

row = row.rstrip(''\ n'')

piid = row.split(''\t'')[0]

如果recs.has_key(piid)为False:

recs [piid] = [ ]

recs [piid] .append(行)


for piid in recs.keys():

best =" ;

for current in recs [piid]:

if best =="":

best = current;

else:

#如果当前记录是正确状态

if current.split(" \t")[1] == current.split(" \t")[6]:

#如果现有记录是正确状态

if best.split(" \t" ;)[1] == best.split(" \t")[6]:

#如果新记录有更新的exp。 date

if current.split(" \t")[5] best.split(" \t")[5]:

best = current

else:

best = current

else:

#如果现有记录没有正确的状态

#和新记录有更新的exp。 date

if best.split(" \t")[1]!= best.split(" \t")[6]和

当前.split(" \t")[5] best.split(" \t")[5]:

best = current


打印得最好

############################################ ###################################

结束Python代码

############################################### #### ########################

I''m new to Python and fairly experienced in Perl, although that
experience is limited to the things I use daily.

I wrote the same script in both Perl and Python, and the output is
identical. The run speed is similar (very fast) and the line count is
similar.

Now that they''re both working, I was looking at the code and wondering
what Perl-specific and Python-specific improvements to the code would
look like, as judged by others more knowledgeable in the individual
languages.

I am not looking for the smallest number of lines, or anything else
that would make the code more difficult to read in six months. Just
any instances where I''m doing something inefficiently or in a "bad"
way.

I''m attaching both the Perl and Python versions, and I''m open to
comments on either. The script reads a file from standard input and
finds the best record for each unique ID (piid). The best is defined
as follows: The newest expiration date (field 5) for the record with
the state (field 1) which matches the desired state (field 6). If
there is no record matching the desired state, then just take the
newest expiration date.

Thanks for taking the time to look at these.

Shawn

################################################## ########################
Perl code:
################################################## ########################
#! /usr/bin/env perl

use warnings;
use strict;

my $piid;
my $row;
my %input;
my $best;
my $curr;

foreach $row (<>){

chomp($row);
$piid = (split(/\t/, $row))[0];

push ( @{$input{$piid}}, $row );
}

for $piid (keys(%input)){

$best = "";

for $curr (@{$input{$piid}}){
if ($best eq ""){
$best = $curr;
}else{
#If the current record is the correct state

if ((split(/\t/, $curr))[1] eq (split(/\t/, $curr))[6]){
#If existing record is the correct state
if ((split(/\t/, $best))[1] eq (split(/\t/, $curr))[6]){
if ((split(/\t/, $curr))[5] gt (split(/\t/, $best))[5]){
$best = $curr;
}
}else{
$best = $curr;
}
}else{
#if the existing record does not have the correct state
#and the new one has a newer expiration date
if (((split(/\t/, $best))[1] ne (split(/\t/, $curr))[6]) and
((split(/\t/, $curr))[5] gt (split(/\t/, $best))[5])){
$best = $curr;
}
}
}
}
print "$best\n";
}

################################################## ########################
End Perl code
################################################## ########################


################################################## ########################
Python code
################################################## ########################

#! /usr/bin/env python

import sys

input = sys.stdin

recs = {}

for row in input:
row = row.rstrip(''\n'')
piid = row.split(''\t'')[0]
if recs.has_key(piid) is False:
recs[piid] = []
recs[piid].append(row)

for piid in recs.keys():
best = ""
for current in recs[piid]:
if best == "":
best = current;
else:
#If the current record is the correct state
if current.split("\t")[1] == current.split("\t")[6]:
#If the existing record is the correct state
if best.split("\t")[1] == best.split("\t")[6]:
#If the new record has a newer exp. date
if current.split("\t")[5] best.split("\t")[5]:
best = current
else:
best = current
else:
#If the existing record does not have the correct state
#and the new record has a newer exp. date
if best.split("\t")[1] != best.split("\t")[6] and
current.split("\t")[5] best.split("\t")[5]:
best = current

print best
################################################## ########################
End Python code
################################################## ########################

推荐答案

piid ;

我的
piid;
my


行;

我的%输入;

我的
row;
my %input;
my


best;

my
best;
my


这篇关于Perl和Python,一个实用的并排示例。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆