用python 找到不存在的数字
2008-03-31 18:06
316 查看
先用bash脚本排序,然后用python脚本找出丢失的数字。当然,也可以完全在python脚本中完成。
#!/usr/bin/python
# filename:findlost.py
# author: zhouhh
# http://blog.csdn.net/ablo_zhou
# email:ablozhou@gmail.com
# date:2008.3.31
# Find out what number is lost from a file
import os
infilename = raw_input("in file name [rawpack]:");
outfilename = raw_input("out file name [outfile]:");
if infilename =="" :
infilename="rawpack";
if outfilename =="" :
outfilename="outfile";
cmd = ("sort.sh",infilename,outfilename)
os.system(' '.join(cmd))
f = open(outfilename);
i=0
pkid = 0
count = 0
while True :
line = f.readline()
if len(line) == 0:
f.close()
break
pkid = int(line)
if pkid>i:
print "lost package ",i
i+=1
count +=1
i+=1
print "================================="
print "total package num is",pkid,",lost package count:",count
执行结果:
$ ./findlost.py
in file name [rawpack]:
out file name [outfile]:
./sort.sh,rawpack,outfile
lost package 1707
lost package 2126
lost package 2139
lost package 2278
lost package 2280
lost package 2475
lost package 2763
lost package 3014
lost package 3072
lost package 3165
lost package 3271
=================================
total package num is 3778 ,lost package count: 11
#!/usr/bin/python
# filename:findlost.py
# author: zhouhh
# http://blog.csdn.net/ablo_zhou
# email:ablozhou@gmail.com
# date:2008.3.31
# Find out what number is lost from a file
import os
infilename = raw_input("in file name [rawpack]:");
outfilename = raw_input("out file name [outfile]:");
if infilename =="" :
infilename="rawpack";
if outfilename =="" :
outfilename="outfile";
cmd = ("sort.sh",infilename,outfilename)
os.system(' '.join(cmd))
f = open(outfilename);
i=0
pkid = 0
count = 0
while True :
line = f.readline()
if len(line) == 0:
f.close()
break
pkid = int(line)
if pkid>i:
print "lost package ",i
i+=1
count +=1
i+=1
print "================================="
print "total package num is",pkid,",lost package count:",count
执行结果:
$ ./findlost.py
in file name [rawpack]:
out file name [outfile]:
./sort.sh,rawpack,outfile
lost package 1707
lost package 2126
lost package 2139
lost package 2278
lost package 2280
lost package 2475
lost package 2763
lost package 3014
lost package 3072
lost package 3165
lost package 3271
=================================
total package num is 3778 ,lost package count: 11
相关文章推荐
- 使用python的ConfigParser模块方便的读取配置文件
- eclipse 安装python 插件 pydev
- 用Python快速编写网站遍历程序
- [工具推荐]pynpp - Python Plugins for Notepad++
- 收集Python 各种方面的技巧片段
- Python正则表达式操作指南
- C Java PHP Perl Python的程序代码美化工具使用
- [Python学习]Unicode及编码处理心得
- 使用 python 开发 Web Service
- python模块之codecs
- Mod_python 3.2.8中文手册
- python源码中使用中文注释
- python集成开发环境Ulipad的安装指南
- 我的 Python 主机空间
- C运算溢出怎么办?
- Python和Endicia.com的打印费用
- Tkinter之四(TCL/Tk在Python上的移植)
- Python中的bool
- Python is just the language for you
- 开始翻译这本书