📜  Python中的 sys.maxint

📅  最后修改于: 2022-05-13 01:54:53.267000             🧑  作者: Mango

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))


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))


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)))


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))


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)))


输出
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 版支持此常量。常数表示的值可以计算为:

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 - 
maxint - 1 :9223372036854775807 - 
maxint + 1 :9223372036854775807 - 

此常量已从Python 3中删除,因为此版本中的整数被认为是任意长度的。如果在Python 3 中使用此常量,则会出现以下错误。考虑必须从列表中找出最小值元素的相同示例。

蟒蛇3

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的最大可能整数值,并且它表示平台的指针大小。此 maxsize 被认为是限制各种数据结构(如字符串和列表)的大小。要注意的另一件事是,在Python 3 中intlong int合并为一个。请查看下面的示例以更好地理解。

蟒蛇3

# 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 - 
maxint - 1 :9223372036854775807 - 
maxint + 1 :9223372036854775807 -