Perl与Python处理文本数值的一个比较
最近有意地用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 #!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 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |