Python教程:sys模塊中maxsize()的方法

来源:https://www.cnblogs.com/xxpythonxx/archive/2023/08/31/17670245.html
-Advertisement-
Play Games

本節內容 列表、元組操作 字元串操作 字典操作 集合操作 文件操作 字元編碼與轉碼 1. 列表、元組操作 列表是我們最以後最常用的數據類型之一,通過列表可以對數據實現最方便的存儲、修改等操作 定義列表 names = ['Alex',"Tenglan",'Eric'] 通過下標訪問列表中的元素,下標 ...


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

Py_ssize_t是一個整數,它給出了變數可以取的最大值。大小因操作系統的位而異。

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

sys.maxsize 方法

sys.maxsize()

返回:此方法根據平臺類型返回最大大小值Py_ssize_t。

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

要實現方法sys.maxsize()並檢查最大大小值,我們可以導入sys模塊並使用方法maxsize()。根據平臺架構類型,sys.maxsize()方法在控制臺上返回其最大值大小。

下麵是32位和64位操作系統的實現,並運行相同的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() 方法

為了檢查我們系統的最大大小,我們可以使用range()方法來傳遞列表的最大大小,然後檢查它的長度。類似地,在第二個例子中,我們超過了最大大小,Python解釋器捕獲了異常並返回int too large to convert to C ssize_t錯誤。

在下麵的例子中,我們可以觀察到對Py_ssize_t設置限制的效果。不可能索引一個元素大於其大小的列表,因為它不接受非Py_ssize_t。

關於字典數據結構,Py_ssize_t使用哈希,因為Python沒有使用LinkedList來實現它。類似地,字典中的大小不能大於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小白學習交流群: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作為整數。如果我們使用這個方法或常量,我們將得到下麵的AttributeError: module 'sys' has no attribute 'maxint'。

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

第一個實現展示了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 執行

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中,當我們讀取包含巨大欄位的CSV文件時,它可能會拋出一個異常,說_csv.Error: field larger than field limit。適當的解決方案是不要跳過一些欄位及其行。

要分析CSV,我們需要增加field_size_limit。為此,我們需要實現以下源代碼。

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)

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • ### 歡迎訪問我的GitHub > 這裡分類和彙總了欣宸的全部原創(含配套源碼):[https://github.com/zq2599/blog_demos](https://github.com/zq2599/blog_demos) ### 題目描述 - 難度:中等 - 給定一個不含重覆數字的數 ...
  • 這些是一些常見的 Linux 命令,用於處理文件、進程、網路、用戶、系統管理等各種任務。根據你的需要,你可能會經常使用其中一些命令來管理和操作你的 Linux 系統。每個命令都有更多的選項和用法,你可以通過在終端中運行 man 命令名 來查看命令的詳細幫助文檔。 ...
  • 1. Python中常用的庫有哪些,作用分別是什麼 requests: requests 是一個用於發送 HTTP 請求的庫,它提供了簡單而優雅的 API,可以輕鬆地發送 GET、POST、PUT、DELETE 等請求,並處理響應數據。它支持會話管理、身份驗證、文件上傳等常見的 HTTP 功能,是進 ...
  • Go語言中的slice表示一個具有相同類型元素的可變長序列,語言本身提供了兩個操作方法: 1. 創建:make([]T,len,cap) 2. 追加: append(slice, T ...) 同時slice支持隨機訪問。本篇文章主要對slice的具體實現進行總結。 ## 1. 數據結構 go語言的 ...
  • Java - ThreadPoolExecutor源碼分析 1. 為什麼要自定義線程池 首先ThreadPoolExecutor中,一共提供了7個參數,每個參數都是非常核心的屬性,線上程池去執行任務時,每個參數都有決定性的作用。 但是如果直接採用JDK提供的方式去構建,可見設置的核心參數最多就兩個, ...
  • # Python的數據類型的內置方法 - 可變與不可變數據類型 - 字典的內置方法 - 元組的內置方法 - 集合的內置方法 ## 可變與不可變數據類型 ### 常見的數據類型 ```python 常見的數據結構類型:鏈表、單鏈表、雙鏈表、迴圈列表、(C實現的 指針)、棧、樹、二叉樹、平衡二叉樹、完全 ...
  • 由於瀏覽器可以迅速地解析JSON對象,它們有助於在客戶端和伺服器之間傳輸數據。本文將描述如何使用Python的JSON模塊來傳輸和接收JSON數據。 ### JavaScript Object Notation JSON (JavaScript Object Notation)是一種用於數據交換的語 ...
  • ### 拾取和解除拾取 對象的序列化是將對象轉換為比特形式的方法,這樣我們就可以在硬碟上保存對象的狀態。儘管許多語言都為我們提供了對象序列化的方式,但Python在所有的語言中更加靈活。 在Python中,對象序列化被稱為pickling,而去序列化被稱為unpickling。我們在 Python ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...