欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2023年09月28日 09:56:48   作者:python教程  
這篇文章主要為大家介紹了Python sys模塊中maxsize()方法教程示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

sys.maxsize 方法

在Python中,sys模塊有一個(gè)名為maxsize()的方法。這個(gè)方法返回一個(gè)變量Py_ssize_t可以容納的最大值。

Py_ssize_t是一個(gè)整數(shù),它給出了變量可以取的最大值。大小因操作系統(tǒng)的位而異。

32位的大小為(2 power 31)-1,64位的大小為(2 power 63)-1。

sys.maxsize()

返回:此方法根據(jù)平臺(tái)類型返回最大大小值Py_ssize_t。

代碼1:使用 sys.maxsize() 方法

要實(shí)現(xiàn)方法sys.maxsize()并檢查最大大小值,我們可以導(dǎo)入sys模塊并使用方法maxsize()。根據(jù)平臺(tái)架構(gòu)類型,sys.maxsize()方法在控制臺(tái)上返回其最大值大小。

下面是32位和64位操作系統(tǒng)的實(shí)現(xiàn),并運(yùn)行相同的sys.maxsize()方法。

32-Bit平臺(tái)

# 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平臺(tái)

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()方法來傳遞列表的最大大小,然后檢查它的長度。類似地,在第二個(gè)例子中,我們超過了最大大小,Python解釋器捕獲了異常并返回int too large to convert to C ssize_t錯(cuò)誤。

在下面的例子中,我們可以觀察到對(duì)Py_ssize_t設(shè)置限制的效果。不可能索引一個(gè)元素大于其大小的列表,因?yàn)樗唤邮芊荘y_ssize_t。

關(guān)于字典數(shù)據(jù)結(jié)構(gòu),Py_ssize_t使用哈希,因?yàn)镻ython沒有使用LinkedList來實(shí)現(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() 對(duì)比 sys.maxint 方法

sys.maxint()方法不再支持Python 3作為整數(shù)。如果我們使用這個(gè)方法或常量,我們將得到下面的AttributeError: module ‘sys’ has no attribute ‘maxint’。

為了在Python 3.0中克服這個(gè)問題,引入了另一個(gè)常量sys.maxsize,我們知道它會(huì)返回Py_ssize_t的最大值。在Python 3中,int和long int是合并的。

第一個(gè)實(shí)現(xiàn)展示了AttributeError的示例,第二個(gè)源代碼揭示了對(duì)maxint的更好理解。

屬性錯(cuò)誤

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文件時(shí),它可能會(huì)拋出一個(gè)異常,說_csv.Error: field larger than field limit。適當(dāng)?shù)慕鉀Q方案是不要跳過一些字段及其行。

要分析CSV,我們需要增加field_size_limit。為此,我們需要實(shí)現(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()方法教程示例的詳細(xì)內(nèi)容,更多關(guān)于Python sys模塊maxsize方法的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Python爬蟲基礎(chǔ)之爬蟲的分類知識(shí)總結(jié)

    Python爬蟲基礎(chǔ)之爬蟲的分類知識(shí)總結(jié)

    來給大家講python爬蟲的基礎(chǔ)啦,首先我們從爬蟲的分類開始講起,下文有非常詳細(xì)的知識(shí)總結(jié),對(duì)正在學(xué)習(xí)python的小伙伴們很有幫助,需要的朋友可以參考下
    2021-05-05
  • Pycharm 常用快捷鍵大全(全網(wǎng)最全)

    Pycharm 常用快捷鍵大全(全網(wǎng)最全)

    本文詳細(xì)介紹了Pycharm中多種提高編程效率的快捷鍵操作,包括代碼格式化、代碼合并、修正代碼警告等,適合Python開發(fā)者使用,感興趣的可以了解一下
    2024-11-11
  • python獲取當(dāng)前用戶的主目錄路徑方法(推薦)

    python獲取當(dāng)前用戶的主目錄路徑方法(推薦)

    下面小編就為大家?guī)硪黄猵ython獲取當(dāng)前用戶的主目錄路徑方法(推薦)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-01-01
  • python 保存float類型的小數(shù)的位數(shù)方法

    python 保存float類型的小數(shù)的位數(shù)方法

    今天小編就為大家分享一篇python 保存float類型的小數(shù)的位數(shù)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-10-10
  • 漂亮的Django Markdown富文本app插件的實(shí)現(xiàn)

    漂亮的Django Markdown富文本app插件的實(shí)現(xiàn)

    這篇文章主要介紹了漂亮的Django Markdown富文本app插件的實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2019-01-01
  • Python開多次方根的案例

    Python開多次方根的案例

    這篇文章主要介紹了Python開多次方根的案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2021-03-03
  • Python中asyncio與aiohttp入門教程

    Python中asyncio與aiohttp入門教程

    今天小編就為大家分享一篇關(guān)于Python中asyncio與aiohttp入門教程,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2018-10-10
  • 詳解Python之unittest單元測(cè)試代碼

    詳解Python之unittest單元測(cè)試代碼

    本篇文件主要介紹了詳解Python之unittest測(cè)試代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-01-01
  • Python中fnmatch模塊的使用詳情

    Python中fnmatch模塊的使用詳情

    這篇文章主要介紹了Python中fnmatch模塊的使用詳情,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-11-11
  • Pytorch:Conv2d卷積前后尺寸詳解

    Pytorch:Conv2d卷積前后尺寸詳解

    這篇文章主要介紹了Pytorch:Conv2d卷積前后尺寸,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-02-02

最新評(píng)論