[英]How to find the smallest number in a text file (Python 2.5)
到目前為止,我的代碼找到了文本文件中最大的數字,但它沒有找到最小的數字,當它運行時,最小的數字仍為0,而最大的數字為9997。
integers = open('numbers.txt', 'r') # opens numbers.txt
largestInt = 0 # making variables to store the largest/smallest int
smallestInt = 0
# loop where we check every line for largest/smallest int
for line in integers:
while largestInt <= line.strip():
largestInt = line
while smallestInt >= line.strip():
smallestInt = line
# print results
print "Smallest = ", smallestInt
print "Largest = ", largestInt
numbers.txt
看起來像:
6037
-2228
8712
-5951
9485
8354
1467
8089
559
9439
-4274
9278
-813
1156
-7528
1843
-9329
574
等等。
這有什么不對? 如果我做錯了,或邏輯錯誤,請糾正我。
EDIT
我要感謝@Martijn Pieters和@Gexos解釋我做錯了什么。 我理解為什么我的代碼現在有效!
最終守則:
integers = open('numbers.txt', 'r') # opens numbers.txt
largestInt = 0 # making variables to store the largest/smallest int
smallestInt = 0
# loop where we check every line for largest/smallest int
for line in integers:
if largestInt <= int(line.strip()): # converted a string into an int
largestInt = int(line.strip()) # made that int into largestInt
if smallestInt >= int(line.strip()): # converted a string into an int
smallestInt = int(line.strip()) # made that int into smallestInt
integers.close() # closes the file
# print results
print "Smallest = ", smallestInt
print "Largest = ", largestInt
結果
Smallest = -9993
Largest = 9997
你正在比較字符串,而不是整數; 在比較之前將您的線轉換為整數:
largestInt = float('-inf')
smallestInt = float('inf')
for line in integers:
number = int(line)
if largestInt < number:
largestInt = number
if smallestInt > number:
smallestInt = number
請注意,您要使用if
在這里,不while
; 后者創造了一個循環。
我分別使用float('-inf')
和float('inf')
啟動了largestInt
和smallestInt
,數字保證比其他任何東西都小。 這使得第一次測試largestInt < number
始終為 true,無論第一行是什么數字。
比較字符串是按字典順序進行的,其中字符逐一進行比較; 10
小於2
因為1
在2
之前排序。
您可以使用max()
和min()
內置函數來輕松實現,但效率會低一些,因為內部這些函數也可以執行循環:
numbers = {int(line) for line in integers}
largestInt = max(numbers)
smallestInt = min(numbers)
雖然我通常喜歡使用min
和max
解決方案,但在這種情況下需要兩次線性傳遞,並且需要大量內存開銷。 這是一個需要一個線性傳遞和常量內存的方法:
with open('numbers.txt') as infile:
smallest, largest = '', None
for line in infile:
n = int(line)
smallest = min(n, smallest)
largest = max(n, largest)
print "the smallest number is", smallest
print "the largest number is", largest
正如其他人所說,你需要先將你的行轉換為整數。 此外,如果該數字大於零,您的腳本將不會輸出正確的最小數字。 要解決此問題,請將最大數量和最小數量都設置為文件中的第一個條目。 然后檢查所有其他數字,看看它們是否比當前數字更大/更小。
with open('numbers.txt', 'r') as data_file:
num = int(next(data_file))
min_num, max_num = num, num
for line in data_file:
num = int(line)
if num > max_num:
max_num = num
elif num < min_num:
min_num = num
print 'Smallest number: {}'.format(min_num)
print 'Largest number: {}'.format(max_num)
這也可以通過列表推導來解決:
nums = [int(line) for line in open('numbers.txt', 'r')]
min_num, max_num = min(nums), max(nums)
with open('number.txt') as f:
s = [int(n.strip()) for n in f]
print min(s), max(s)
一些東西。 您需要將該line
轉換為字符串中的int: int(line.strip())
當前您正在將int與字符串進行比較。 您還應該在largestInt = int(line.strip())
賦值,對於smallestInt
。
你不應該使用while
。 while
對於循環,而不是比較。 你應該使用if
。
最后但並非最不重要的是確保最后關閉文件。 integers.close()
integers = open('numbers.txt', 'r')
intList = [int(x) for x in integers.readlines()]
print max(intList), min(intList)
您正在比較字符串,而不是整數。 你需要在最有可能將它們轉換為數字的某個階段調用它們的int
函數。
這是一個不同方法的示例:
with open('numbers.txt', 'r') as f:
integers = [int(n) for n in f.readlines()]
smallest = min(integers)
biggest = max(integers)
使用with
確保文件在列表理解后自動關閉,這是一種很好的做法。 列表理解結果如下:
[6037, -2228, 8712, -5951, 9485, 8354, 1467, 8089, 559, 9439, -4274, 9278, -813, 1156, -7528, 1843, -9329, 574]
然后在該列表上調用min
和max
。
將tinyInt設置為0時需要注意的一件事。如果文本文檔包含的最小數字為1,那么您仍然會以0作為答案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.