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

Python中的內(nèi)存管理之python list內(nèi)存使用詳解

 更新時間:2021年09月04日 13:11:09   作者:be5yond  
這篇文章主要介紹了Python中的內(nèi)存管理之python list內(nèi)存使用詳解,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下

前言

使用 Python 的時候,我們知道 list 是一個長度可變對的數(shù)組, 可以通過 insert,append 和 extend 輕易的拓展其中的元素個數(shù)。 也可以使用運算符 如: [1] + [2] 生成新的數(shù)組[1, 2]

extend()、 "+"、"+=" 的區(qū)別

  • "+"將兩個 list 相加,會返回到一個新的 list 對象
  • append 在原 list 上進行修改,沒有返回值

從以下代碼可以看到, 調(diào)用 b = b + [3, 4] 之后, 通過id(b) 查看 b 變成了一個新對象。

In [5]: b = [1, 2]
In [6]: id(b)
Out[6]: 1628740249224
 
In [7]: b = b + [3, 4]
In [8]: id(b)
Out[8]: 1628740456520

 使用extend() 完成相同的步驟, 可以看到 對象c 的id保持和原來的一致

In [9]: c = [1, 2]
In [10]: id(c)
Out[10]: 1628740392584
 
In [11]: c.extend([3, 4])
In [12]: id(c)
Out[12]: 1628740392584

 使用 "+="  連接列表, 看到效果和 extend() 是相同的。

In [1]: a = [1, 2]
In [2]: id(a)
Out[2]: 1628740021448
 
In [3]: a += [3, 4]
In [4]: id(a)
Out[4]: 1628740021448

 結(jié)論: 減少內(nèi)存的拷貝, 修改一個列表的數(shù)據(jù)時, 應避免使用 list1 = list1 + list2 這樣的語法。 

List的內(nèi)存使用

一個示例:

In [1]: import sys
 
In [2]: lst1 = [1]
In [3]: lst2 = []
In [4]: lst2.append(1)
 
In [5]: lst1 == lst2
Out[5]: True
 
In [6]: sys.getsizeof(lst1)
Out[6]: 72
In [7]: sys.getsizeof(lst2)
Out[7]: 96

可以看到,lst1 == lst2, 但是當使用 sys.getsizeof 獲取對象的內(nèi)存大小時, 兩者卻是不同的。

如下圖所示, list_a 長度為4, 當執(zhí)行 append(4) 時, 底層的數(shù)據(jù)長度其實申請了4個元素的空間,當再次執(zhí)行 append(5) 的時候,不需要再次申請內(nèi)存。

 

因為 執(zhí)行 append() 操作時,Python將一次拓展N個元素的內(nèi)存,因為一個 append 操作很可能是很多 append 操作的開始,通過額外分配內(nèi)存來減少可能的內(nèi)存分配和內(nèi)存copy的次數(shù)。

In [1]: import sys
 
In [2]: l = []
   ...: print(f'list initial size {sys.getsizeof(l)}')
   ...: for i in range(80):
   ...:     cur_size = sys.getsizeof(l)
   ...:     l.append(i)
   ...:     new_size = sys.getsizeof(l)
   ...:     print(f'list len {i+1}:\t current_size {new_size}\t new_allocated 8 * {(new_size-cur_size)/8}')
   ...:
list initial size 64
list len 1:      current_size 96         new_allocated 8 * 4.0
list len 2:      current_size 96         new_allocated 8 * 0.0
list len 3:      current_size 96         new_allocated 8 * 0.0
list len 4:      current_size 96         new_allocated 8 * 0.0
list len 5:      current_size 128        new_allocated 8 * 4.0
list len 6:      current_size 128        new_allocated 8 * 0.0
list len 7:      current_size 128        new_allocated 8 * 0.0
list len 8:      current_size 128        new_allocated 8 * 0.0
list len 9:      current_size 192        new_allocated 8 * 8.0
list len 10:     current_size 192        new_allocated 8 * 0.0
list len 11:     current_size 192        new_allocated 8 * 0.0
list len 12:     current_size 192        new_allocated 8 * 0.0
list len 13:     current_size 192        new_allocated 8 * 0.0
list len 14:     current_size 192        new_allocated 8 * 0.0
list len 15:     current_size 192        new_allocated 8 * 0.0
list len 16:     current_size 192        new_allocated 8 * 0.0
list len 17:     current_size 264        new_allocated 8 * 9.0
list len 18:     current_size 264        new_allocated 8 * 0.0
list len 19:     current_size 264        new_allocated 8 * 0.0
list len 20:     current_size 264        new_allocated 8 * 0.0
list len 21:     current_size 264        new_allocated 8 * 0.0
list len 22:     current_size 264        new_allocated 8 * 0.0
list len 23:     current_size 264        new_allocated 8 * 0.0
list len 24:     current_size 264        new_allocated 8 * 0.0
list len 25:     current_size 264        new_allocated 8 * 0.0
list len 26:     current_size 344        new_allocated 8 * 10.0
list len 27:     current_size 344        new_allocated 8 * 0.0
list len 28:     current_size 344        new_allocated 8 * 0.0
list len 29:     current_size 344        new_allocated 8 * 0.0
list len 30:     current_size 344        new_allocated 8 * 0.0
list len 31:     current_size 344        new_allocated 8 * 0.0
list len 32:     current_size 344        new_allocated 8 * 0.0
list len 33:     current_size 344        new_allocated 8 * 0.0
list len 34:     current_size 344        new_allocated 8 * 0.0
list len 35:     current_size 344        new_allocated 8 * 0.0
list len 36:     current_size 432        new_allocated 8 * 11.0
list len 37:     current_size 432        new_allocated 8 * 0.0
list len 38:     current_size 432        new_allocated 8 * 0.0
list len 39:     current_size 432        new_allocated 8 * 0.0
list len 40:     current_size 432        new_allocated 8 * 0.0
list len 41:     current_size 432        new_allocated 8 * 0.0
list len 42:     current_size 432        new_allocated 8 * 0.0
list len 43:     current_size 432        new_allocated 8 * 0.0
list len 44:     current_size 432        new_allocated 8 * 0.0
list len 45:     current_size 432        new_allocated 8 * 0.0
list len 46:     current_size 432        new_allocated 8 * 0.0
list len 47:     current_size 528        new_allocated 8 * 12.0
list len 48:     current_size 528        new_allocated 8 * 0.0
list len 49:     current_size 528        new_allocated 8 * 0.0
list len 50:     current_size 528        new_allocated 8 * 0.0
list len 51:     current_size 528        new_allocated 8 * 0.0
list len 52:     current_size 528        new_allocated 8 * 0.0
list len 53:     current_size 528        new_allocated 8 * 0.0
list len 54:     current_size 528        new_allocated 8 * 0.0
list len 55:     current_size 528        new_allocated 8 * 0.0
list len 56:     current_size 528        new_allocated 8 * 0.0
list len 57:     current_size 528        new_allocated 8 * 0.0
list len 58:     current_size 528        new_allocated 8 * 0.0
list len 59:     current_size 640        new_allocated 8 * 14.0
list len 60:     current_size 640        new_allocated 8 * 0.0
list len 61:     current_size 640        new_allocated 8 * 0.0
list len 62:     current_size 640        new_allocated 8 * 0.0
list len 63:     current_size 640        new_allocated 8 * 0.0
list len 64:     current_size 640        new_allocated 8 * 0.0
list len 65:     current_size 640        new_allocated 8 * 0.0
list len 66:     current_size 640        new_allocated 8 * 0.0
list len 67:     current_size 640        new_allocated 8 * 0.0
list len 68:     current_size 640        new_allocated 8 * 0.0
list len 69:     current_size 640        new_allocated 8 * 0.0
list len 70:     current_size 640        new_allocated 8 * 0.0
list len 71:     current_size 640        new_allocated 8 * 0.0
list len 72:     current_size 640        new_allocated 8 * 0.0
list len 73:     current_size 768        new_allocated 8 * 16.0
list len 74:     current_size 768        new_allocated 8 * 0.0
list len 75:     current_size 768        new_allocated 8 * 0.0
list len 76:     current_size 768        new_allocated 8 * 0.0
list len 77:     current_size 768        new_allocated 8 * 0.0
list len 78:     current_size 768        new_allocated 8 * 0.0
list len 79:     current_size 768        new_allocated 8 * 0.0
list len 80:     current_size 768        new_allocated 8 * 0.0

通過觀察可以發(fā)現(xiàn), 列表從0 增加到 80長度的過程中, 新申請的內(nèi)存長度為 [4, 4, 8, 9, 10, 11, 12, 13, 14, 16] 。 反之, 當執(zhí)行 remove 或者 pop 減少列表中的數(shù)據(jù)時, 列表也會自動縮容。

  • 擴容條件 ,新長度大于底層數(shù)組長度;
  • 縮容條件 ,新長度小于底層數(shù)組長度的一半;

結(jié)論: 避免使用類似 append 語法初始化列表, 優(yōu)先使用列表表達式

# Bad ❌
list_a = []
for i in range(50):
    list_a.append(i)
 
# Good ✔️
list_b = [i for i in range(50)]

結(jié)論:

① 避免使用 "+" 修改數(shù)組

② 盡量避免多次使用 append 函數(shù)

到此這篇關于Python中的內(nèi)存管理之python list內(nèi)存使用詳解的文章就介紹到這了,更多相關python list內(nèi)存使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • 一文詳解Python中的重試機制

    一文詳解Python中的重試機制

    本文將給大家介紹一個第三方庫-Tenacity(標題中的重試機制并并不準確,它不是 Python 的內(nèi)置模塊,因此并不能稱之為機制),它實現(xiàn)了幾乎我們可以使用到的所有重試場景,快跟隨小編一起學習一下吧
    2022-07-07
  • Python入門及進階筆記 Python 內(nèi)置函數(shù)小結(jié)

    Python入門及進階筆記 Python 內(nèi)置函數(shù)小結(jié)

    這篇文章主要介紹了Python的內(nèi)置函數(shù)小結(jié),需要的朋友可以參考下
    2014-08-08
  • Python如何輸出百分比

    Python如何輸出百分比

    這篇文章主要介紹了Python 如何輸出百分比,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-07-07
  • Python 使用SMTP發(fā)送郵件的代碼小結(jié)

    Python 使用SMTP發(fā)送郵件的代碼小結(jié)

    python的smtplib提供了一種很方便的途徑發(fā)送電子郵件。它對smtp協(xié)議進行了簡單的封裝,需要的朋友可以參考下
    2016-09-09
  • Python使用pathlib庫實現(xiàn)優(yōu)雅的處理路徑

    Python使用pathlib庫實現(xiàn)優(yōu)雅的處理路徑

    如果你需要在 Python 里進行文件處理,那么標準庫中的os和os.path兄弟倆一定是你無法避開的兩個模塊,本文主要來和大家聊聊如何使用pathlib庫實現(xiàn)優(yōu)雅的處理路徑,感興趣的可以了解下
    2023-12-12
  • python下MySQLdb用法實例分析

    python下MySQLdb用法實例分析

    這篇文章主要介紹了python下MySQLdb用法,實例分析了Python中MySQLdb的安裝及使用技巧,包括增刪改查及亂碼處理的相關技巧,需要的朋友可以參考下
    2015-06-06
  • Python實現(xiàn)將元組中的元素作為參數(shù)傳入函數(shù)的操作

    Python實現(xiàn)將元組中的元素作為參數(shù)傳入函數(shù)的操作

    這篇文章主要介紹了Python實現(xiàn)將元組中的元素作為參數(shù)傳入函數(shù)的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-06-06
  • Python回溯法(Backtracking)的具體使用

    Python回溯法(Backtracking)的具體使用

    在Python中,我們可以應用回溯法解決各種問題,如八皇后問題、子集問題等,本文就來介紹一下Python回溯法(Backtracking)的具體使用,感興趣的可以了解一下
    2023-12-12
  • Python?設計模式中命令模式

    Python?設計模式中命令模式

    這篇文章主要介紹了Python?設計模式中的命令模式,命令模式的目的是解耦調(diào)用操作的對象和提供實現(xiàn)的對象,下文介紹具有一定參考價值,需要的小伙伴可以參考一下
    2022-02-02
  • python按鍵按住不放持續(xù)響應的實例代碼

    python按鍵按住不放持續(xù)響應的實例代碼

    今天小編就為大家分享一篇python按鍵按住不放持續(xù)響應的實例代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-07-07

最新評論

抚宁县| 习水县| 安陆市| 鄂伦春自治旗| 呼玛县| 秭归县| 长寿区| 三河市| 涿州市| 鄂尔多斯市| 武义县| 黎川县| 郸城县| 九江县| 兰西县| 扎兰屯市| 乌拉特前旗| 满洲里市| 垣曲县| 墨竹工卡县| 手游| 浑源县| 习水县| 龙口市| 利津县| 司法| 瑞丽市| 芦山县| 清丰县| 高雄市| 阿克苏市| 天水市| 望城县| 高雄县| 中江县| 洪泽县| 惠来县| 沙田区| 扬中市| 津市市| 丘北县|