我在文件夾中有一個文本文件,該文件有很多數字的數據。我需要使用 Python 從該文件的每一行中找到最小值和最大值。結果應該是這樣的:
.txt 文件中的示例數字
10 2 3 5 9 12 15
5 9 4 8 10 98 15
23 19 89 71 56 20 11
結果像這樣
[(min,max)from first row, (min,max)from second row,.........]
預期結果
[(2,15),(4,98),(11,89),.....]
循環線,分裂。轉換為 int 並使用 min / max
with open ('in.txt') as f:
data = []
for line in f:
numbers = [int(x) for x in line.strip().split()]
data.append((min(numbers),max(numbers)))
print(data)
輸出
[(2, 15), (4, 98), (11, 89)]
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句