最近有意地用Python写一些原来习惯于用Perl写的脚本。发现Perl还是有其自由之处,在处理数值与字符串时有很大的方便。我所要做的工作是简单根据广告类别对模拟实验结果进行平均。 这个任务用Perl编写需要36行代码写成,Python需要43行。即使两者都尽可能一个语句一行,仍然是Python需要的行数多(Perl包括其括号)。 Python的额外工作在于: 1)字典Key的初始化(Python不支持Autovivi) 2) Python分别字符串与数值,两者不能统一处理,经常需要相互转换。3)Python (3.0之前版本)在输出非空格分隔的数据时需要额外处理(Perl的$,与$\变量可方便做这件事)
总结: Perl在字符串与数值文本的处理中还是有其强大之处吧, Python未必就比Perl好!
Python 的问题1)可以通过get方法避免: d[i] = d.get(i,0) + record_num
附两个程序:
#!usr/bin/perl
use strict;
my %dict_all_num;
my %dict_click_num;
my %dict_uniq_click_num;
while (<>) {
chomp;
my ($record_num,$cat,$click_num,$uniq_click_num) = split /,/;
$dict_all_num{$cat} += $record_num;
$dict_click_num{$cat} += $click_num;
$dict_uniq_click_num{$cat} += $uniq_click_num;
}
local $,= ",";
local $\ = "\n";
for my $cat (keys %dict_all_num) {
$dict_all_num{$cat} /= 3;
$dict_click_num{$cat} /= 3;
$dict_uniq_click_num{$cat} /= 3;
print int($dict_all_num{$cat}),
$cat,
int($dict_click_num{$cat}),
int($dict_uniq_click_num{$cat}),
$dict_click_num{$cat} / $dict_all_num{$cat},
$dict_uniq_click_num{$cat} / $dict_all_num{$cat};
}
#!usr/bin/python import sys input_file = sys.argv[1] dict_all_num = {} dict_click_num = {} dict_uniq_click_num = {} for line in open(input_file,'r'): arr = line.rstrip().split(',') record_num = int(arr[0]) cat = arr[1] click_num = int(arr[2]) uniq_click_num = int(arr[3]) if not cat in dict_all_num: dict_all_num[cat] = 0 dict_click_num[cat] = 0 dict_uniq_click_num[cat] = 0 dict_all_num[cat] += record_num dict_click_num[cat] += click_num dict_uniq_click_num[cat] += uniq_click_num for cat in dict_all_num: dict_all_num[cat] /= 3 dict_click_num[cat] /= 3 dict_uniq_click_num[cat] /= 3 lst = [ str(dict_all_num[cat]), cat, str(dict_click_num[cat]), str(dict_uniq_click_num[cat]), str(float(dict_click_num[cat]) / dict_all_num[cat]), str(float(dict_uniq_click_num[cat]) / dict_all_num[cat]) ] output_line = ",".join(lst) print output_line
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。