- 论坛徽章:
- 11
|
回复 1# hothat007
虽然有很多写法,但楼主的写法不够Pythonic - #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- import itertools
- def getitems(fobj):
- for idx, line in enumerate(fobj):
- items = line.split()
- if len(items) != 2:
- continue
- # (行索引,一列浮点数,二列整数)
- yield idx, float(items[0]), int(items[1])
- def main():
- with open('tmp.txt') as fp:
- print max(
- itertools.ifilter(
- lambda e: 28.0 <= e[1] <= 29.0,
- getitems(fp)),
- key=lambda e: e[2])
- if __name__ == '__main__':
- main()
复制代码 附近是自己构造的测试数据,和你的应该类似,原理不变。
tmp.txt.gz
(7.61 KB, 下载次数: 4)
|
|