Python sys模塊中maxsize()方法教程示例
sys.maxsize 方法
在Python中,sys模塊有一個名為maxsize()的方法。這個方法返回一個變量Py_ssize_t可以容納的最大值。
Py_ssize_t是一個整數(shù),它給出了變量可以取的最大值。大小因操作系統(tǒng)的位而異。
32位的大小為(2 power 31)-1,64位的大小為(2 power 63)-1。
sys.maxsize()
返回:此方法根據(jù)平臺類型返回最大大小值Py_ssize_t。
代碼1:使用 sys.maxsize() 方法
要實現(xiàn)方法sys.maxsize()并檢查最大大小值,我們可以導(dǎo)入sys模塊并使用方法maxsize()。根據(jù)平臺架構(gòu)類型,sys.maxsize()方法在控制臺上返回其最大值大小。
下面是32位和64位操作系統(tǒng)的實現(xiàn),并運行相同的sys.maxsize()方法。
32-Bit平臺
# import the sys module to use the maxsize() method
import sys
# returns the maximum size
size = sys.maxsize
print("The maximum size of a 32-bit platform is:" , size)
#輸出:
The maximum size of a 32-bit platform is: 214748364764-Bit平臺
import sys
# returns the maximum size
size = sys.maxsize
print("The maximum size of a 32-bit platform is:" , size)
#輸出:
The maximum size of a 64-bit platform is: 9223372036854775807代碼2:檢查列表的最大大小 sys.maxsize() 方法
為了檢查我們系統(tǒng)的最大大小,我們可以使用range()方法來傳遞列表的最大大小,然后檢查它的長度。類似地,在第二個例子中,我們超過了最大大小,Python解釋器捕獲了異常并返回int too large to convert to C ssize_t錯誤。
在下面的例子中,我們可以觀察到對Py_ssize_t設(shè)置限制的效果。不可能索引一個元素大于其大小的列表,因為它不接受非Py_ssize_t。
關(guān)于字典數(shù)據(jù)結(jié)構(gòu),Py_ssize_t使用哈希,因為Python沒有使用LinkedList來實現(xiàn)它。類似地,字典中的大小不能大于Py_ssize_t的大小。
最大尺寸
import sys
size = sys.maxsize
# creates the max length
list = range(size)
# returns the length of a list
print("The maximum length of a list:" , len(list))
print("List is created successfully")
#輸出:
# maximum size limit on a 64-bit platform
The maximum length of a list: 9223372036854775807
List is created successfully大于最大大小
import sys
size = sys.maxsize
# handles the exception
try:
# creates a list with maximum size + 1
list = range(size + 1)
# check the maximum size
print(len(list))
print("List is created successfully")
# exception if the size goes beyond the maximum size
except Exception as exception:
print("Exception caught: ", exception)
print("List is not created due to above exception")
#Python小白學(xué)習(xí)交流群:711312441
#輸出:
# output shows exception occurs
Exception caught: Python int too large to convert to C ssize_t
List is not created due to above exception代碼3:該 sys.maxsize() 對比 sys.maxint 方法
sys.maxint()方法不再支持Python 3作為整數(shù)。如果我們使用這個方法或常量,我們將得到下面的AttributeError: module ‘sys’ has no attribute ‘maxint’。
為了在Python 3.0中克服這個問題,引入了另一個常量sys.maxsize,我們知道它會返回Py_ssize_t的最大值。在Python 3中,int和long int是合并的。
第一個實現(xiàn)展示了AttributeError的示例,第二個源代碼揭示了對maxint的更好理解。
屬性錯誤
import sys
li = [20, 2, 23, 88, 3, 63, 12]
# sys.maxint is not supported in python 3. We need to use python version < 3.0
min_value = sys.maxint
for i in range(0, len(li)):
if li[i] < min_value:
min_value = li[i]
print("Min value : " + str(min_value))
輸出:
AttributeError: module 'sys' has no attribute 'maxint'maxint 執(zhí)行
import sys
max_int = sys.maxsize
min_int = sys.maxsize - 1
long_int = sys.maxsize + 1
print("Maximum integer size is : " + str(max_int)+" , "+str(type(max_int)))
print("Maximum integer size-1 is :" + str(max_int)+" , "+str(type(min_int)))
print("Maximum integer size+1 is :" + str(max_int)+" , "+str(type(long_int)))
#輸出:
Maximum integer size is : 9223372036854775807 , <class 'int'>
Maximum integer size-1 is :9223372036854775807 , <class 'int'>
Maximum integer size+1 is :9223372036854775807 , <class 'int'>代碼4:在Python中使用 csv.field_size_limit(sys.maxsize)
在Python中,當(dāng)我們讀取包含巨大字段的CSV文件時,它可能會拋出一個異常,說_csv.Error: field larger than field limit。適當(dāng)?shù)慕鉀Q方案是不要跳過一些字段及其行。
要分析CSV,我們需要增加field_size_limit。為此,我們需要實現(xiàn)以下源代碼。
import sys
# to use the field_size_limit method
import csv
maximum_Integer = sys.maxsize
while True:
# read the csv with huge fields
with open('myfile.csv', newline='') as f:
reader = csv.reader(f)
for row in reader:
print(row)
# Here, we reduce the size if there is an overflow error
try:
csv.field_size_limit(maximum_Integer)
break
except OverflowError:
maximum_Integer = int(maximum_Integer/10)以上就是Python sys模塊中maxsize()方法教程示例的詳細內(nèi)容,更多關(guān)于Python sys模塊maxsize方法的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
對python中的os.getpid()和os.fork()函數(shù)詳解
今天小編就為大家分享一篇對python中的os.getpid()和os.fork()函數(shù)詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-08-08
Python3刪除排序數(shù)組中重復(fù)項的方法分析
這篇文章主要介紹了Python3刪除排序數(shù)組中重復(fù)項的方法,結(jié)合實例形式分析了Python3刪除排序數(shù)組重復(fù)項的原理、相關(guān)遍歷及刪除操作技巧,需要的朋友可以參考下2019-01-01
使用python-cv2實現(xiàn)Harr+Adaboost人臉識別的示例
這篇文章主要介紹了使用python-cv2實現(xiàn)Harr+Adaboost人臉識別的示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
Python實現(xiàn)PowerPoint演示文稿到圖片的批量轉(zhuǎn)換
PowerPoint演示文稿作為展示創(chuàng)意、分享知識和表達觀點的重要工具,被廣泛應(yīng)用于教育、商務(wù)匯報及個人項目展示等領(lǐng)域,用Python代碼可以高效地實現(xiàn)PowerPoint演示文稿到圖片的批量轉(zhuǎn)換,從而提升工作效率,文本將介紹如何使用Python實現(xiàn)PowerPoint演示文稿到圖片的轉(zhuǎn)換2024-06-06

