當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python sys.maxint用法及代碼示例

在編程中,maxint /INT_MAX表示可以由整數表示的最大值。在某些情況下,在編程時,我們可能需要分配一個大於任何其他整數值的值。通常,人們會手動分配這些值。例如,考慮一個必須使用for循環找出最小值的整數列表。

Python

# initializing the list 
li = [1, -22, 43, 89, 2, 6, 3, 16] 
  
# assigning a larger value manually 
curr_min = 999999
  
# loop to find minimum value 
for i in range(0, len(li)):
  
    # update curr_min if a value lesser than it is found 
    if li[i] < curr_min:
        curr_min = li[i] 
  
print("The minimum value is " + str(curr_min))
輸出
The minimum value is -22

在上麵的方法中,我們假定999999是列表中的最大可能值,並將其與其他元素進行比較以在找到小於該值的值時進行更新。

Python中的sys模塊

該模塊用於與解釋器進行交互並訪問解釋器維護的變量。它可用於在運行時環境中進行操作。必須像其他軟件包一樣將其導入,以利用其中的函數。 Python的sys模塊提供了各種函數和常量,其中常量maxint可用於設置正整數值,該值肯定大於任何其他整數。看下麵的例子。

Python

# import the module 
import sys 
  
# initializing the list 
li = [1, -22, 43, 89, 2, 6, 3, 16] 
  
# assigning a larger value with  
# maxint constant 
curr_min = sys.maxint 
  
# loop to find minimum value 
for i in range(0, len(li)):
  
    # update curr_min if a value lesser  
    # than it is found 
    if li[i] < curr_min:
        curr_min = li[i] 
  
print("The minimum value is " + str(curr_min))
輸出

The minimum value is -22

在上麵的程序中,使用sys.maxint代替手動分配更大的值。 Python版本2.x支持此常量。常數表示的值可以計算為:

maxint = 231 - 1 (in 32-bit environment)

maxint = 263 - 1 (in 64-bit environment)

在Python 2中,將maxint加1可以得到盡可能長的long int,而在Python 2.7中,從maxint減去1可以得到最小的整數值。

Python

# import the module 
import sys 
  
max_int = sys.maxint 
min_int = sys.maxint-1
long_int = sys.maxint+1
  
print("maxint:"+str(max_int)+" - "+str(type(max_int))) 
print("maxint - 1:"+str(max_int)+" - "+str(type(min_int))) 
print("maxint + 1:"+str(max_int)+" - "+str(type(long_int)))
輸出
maxint:9223372036854775807 - <type 'int'>
maxint - 1:9223372036854775807 - <type 'int'>
maxint + 1:9223372036854775807 - <type 'long'>

該常量已從Python 3中刪除,因為此版本中的整數被認為具有任意長度。如果在Python 3中使用此常量,則將出現以下錯誤。考慮同一示例,其中必須從列表中找出最小值元素。

Python3

import sys 
  
# initializing the list 
li = [1, -22, 43, 89, 2, 6, 3, 16] 
  
# assigning a larger value with maxint constant 
curr_min = sys.maxint 
  
# loop to find minimum value 
for i in range(0, len(li)):
  
    # update curr_min if a value lesser than it is found 
    if li[i] < curr_min:
        curr_min = li[i] 
  
print("The minimum value is " + str(curr_min))

輸出:

AttributeError:module 'sys' has no attribute 'maxint'

由於不再限製整數值,因此刪除了此常數。在Python 3中,引入了與此類似的常量sys.maxsize。這將返回變量類型Py_ssize_t的最大可能整數值,並且它還表示平台的指針大小。該最大大小被認為限製了各種數據結構(如字符串和列表)的大小。需要注意的另一件事是,在Python 3中,int和long int合並為一個。請看下麵的示例,以更好地理解。

Python3

# import the module 
import sys 
  
# using sys.maxsize 
max_int = sys.maxsize 
min_int = sys.maxsize-1
long_int = sys.maxsize+1
  
print("maxint:"+str(max_int)+" - "+str(type(max_int))) 
print("maxint - 1:"+str(max_int)+" - "+str(type(min_int))) 
  
# the data type is represented as int 
print("maxint + 1:"+str(max_int)+" - "+str(type(long_int)))
輸出
maxint:9223372036854775807 - <class 'int'>
maxint - 1:9223372036854775807 - <class 'int'>
maxint + 1:9223372036854775807 - <class 'int'>




注:本文由純淨天空篩選整理自erakshaya485大神的英文原創作品 sys.maxint in Python。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。