返回Max和min不能正常工作

时间:2018-05-15 11:57:57

标签: python python-2.7

这是一个文件:

operation1,83123
operation2,72321
operation3,13288
operation2,12312
operation1,45455
operation2,42543
operation5,344
operation4,61321
operation3,71724
operation5,54546
operation1,12298
operation6,38999
operation3,76798
operation4,53983

我的代码;

f = open('ops.log', 'r')   
data = f.readlines()
print max(data), min(data)

operation6,38999 operation1,12298

为什么它无法返回正确的值

3 个答案:

答案 0 :(得分:0)

假设您想要在逗号后面获取值,您可以执行以下操作:

ID  COMMITED LEVEL      MOBILE NUMBER   FAMILY NUMBER
1   VOTER               234828288       1
3   COMMITED            924792922       2
5   VOTER               79246826234     3

答案 1 :(得分:0)

with open(filename, "r") as infile:
    data = [int(i.split(",")[1]) for i in infile.readlines()]   #Split str by comma and get 1st index

print(max(data))
print(min(data))

<强>输出:

83123
344

答案 2 :(得分:0)

要获取每秒最小和最大列的行,请使用:

with open('ops.log', 'r') as f:
    lines = sorted(f.read().split('\n'), key=lambda x: int(x.split(',')[1]))
    minimum, maximum = lines[0], lines[-1]
    print minimum, maximum
#operation5,344 operation1,83123