最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python sys模塊中maxsize()方法教程示例

 更新時間:2023年09月28日 09:56:48   作者:python教程  
這篇文章主要為大家介紹了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: 2147483647

64-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+Pytorch實戰(zhàn)之彩色圖片識別

    Python+Pytorch實戰(zhàn)之彩色圖片識別

    這篇文章主要為大家詳細介紹了如何利用Python+Pytorch實現(xiàn)彩色圖片識別功能,文中的示例代碼講解詳細,感興趣的小伙伴可以了解一下
    2022-09-09
  • 詳解Python中namedtuple的使用

    詳解Python中namedtuple的使用

    這篇文章主要介紹了Python中namedtuple的使用,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-04-04
  • 對python中的os.getpid()和os.fork()函數(shù)詳解

    對python中的os.getpid()和os.fork()函數(shù)詳解

    今天小編就為大家分享一篇對python中的os.getpid()和os.fork()函數(shù)詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-08-08
  • python處理json文件的四個常用函數(shù)

    python處理json文件的四個常用函數(shù)

    這篇文章主要介紹了python處理json文件的四個常用函數(shù),主要包括json.load()和json.dump()及json.loads()還有json.dumps(),需要的朋友可以參考一下
    2022-07-07
  • python實現(xiàn)布爾型盲注的示例代碼

    python實現(xiàn)布爾型盲注的示例代碼

    這篇文章主要介紹了python實現(xiàn)sql布爾盲注的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04
  • Python以表格形式輸出內(nèi)容到終端的常見方式

    Python以表格形式輸出內(nèi)容到終端的常見方式

    在Python編程語言中,我們經(jīng)常需要在終端輸出數(shù)據(jù),并且有時候需要以表格的形式展示數(shù)據(jù),本文將介紹如何在Python中使用這些庫來輸出表格,需要的可以參考下
    2025-05-05
  • Python3刪除排序數(shù)組中重復(fù)項的方法分析

    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人臉識別的示例

    這篇文章主要介紹了使用python-cv2實現(xiàn)Harr+Adaboost人臉識別的示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-10-10
  • Python實現(xiàn)的凱撒密碼算法示例

    Python實現(xiàn)的凱撒密碼算法示例

    這篇文章主要介紹了Python實現(xiàn)的凱撒密碼算法,簡單介紹了凱撒密碼的概念、原理并結(jié)合實例形式分析了Python實現(xiàn)凱撒密碼算法的相關(guān)定義與使用操作技巧,需要的朋友可以參考下
    2018-04-04
  • Python實現(xiàn)PowerPoint演示文稿到圖片的批量轉(zhuǎn)換

    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

最新評論

江达县| 安平县| 通河县| 黑龙江省| 乡宁县| 共和县| 海安县| 苗栗市| 砚山县| 苍梧县| 仁怀市| 平陆县| 清新县| 东山县| 麦盖提县| 酒泉市| 息烽县| 孟村| 吉木乃县| 林西县| 都昌县| 宁乡县| 永德县| 丁青县| 理塘县| 墨玉县| 白水县| 富裕县| 四子王旗| 白城市| 鄄城县| 周至县| 安平县| 清水县| 印江| 沙河市| 怀柔区| 离岛区| 仪征市| 卢湾区| 延安市|