【Python】python中的數組(list)常用方法


1. 前言

python 中的數組可以說是本人最常用的數據結構了,其中一些方法總是忘記,再次做一下總結。

2. 如何查看某個數據結構的方法

Python 3.9.9 (main, Dec  2 2021, 14:30:08)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> help(list)

執行上面的命令之后,就會顯示出所有該數據結構的內置和外置方法

 |
 |  append(self, object, /)
 |      Append object to the end of the list.
 |  clear(self, /)
 |      Remove all items from list.
 |
 |  copy(self, /)
 |      Return a shallow copy of the list.
 |
 |  count(self, value, /)
 |      Return number of occurrences of value.
 |
 |  extend(self, iterable, /)
 |      Extend list by appending elements from the iterable.
 |
 |  index(self, value, start=0, stop=9223372036854775807, /)
 |      Return first index of value.
 |
 |      Raises ValueError if the value is not present.
 |
 |  insert(self, index, object, /)
 |      Insert object before index.
 |
 |  pop(self, index=-1, /)
 |      Remove and return item at index (default last).
 |
 |      Raises IndexError if list is empty or index is out of range.
 |
 |  remove(self, value, /)
 |      Remove first occurrence of value.
 |
 |      Raises ValueError if the value is not present.
 |
 |  reverse(self, /)
 |      Reverse *IN PLACE*.
 |
 |  sort(self, /, *, key=None, reverse=False)
 |      Sort the list in ascending order and return None.
 |
 |      The sort is in-place (i.e. the list itself is modified) and stable (i.e. the
 |      order of two equal elements is maintained).
 |
 |      If a key function is given, apply it once to each list item and sort them,
 |      ascending or descending, according to their function values.
 |
 |      The reverse flag can be set to sort in descending order.

3. 方法解釋

3.1 概覽

每個函數的基本功能都在表格里有解釋了,現在針對幾個比較特殊的再做一下詳解。另外補充幾個適用於list 的方法

3.2 copy

#list.copy(self)

L1 = [1,2,3,4]
L2 = L1.copy()
print(L2)
# [1,2,3,4]

這個函數用於復制列表,做淺拷貝,與之對應的有深拷貝

import copy
l1 = [1,2,3,4]
l2 = copy.deepcopy(l1)

深淺拷貝可以參考下這個文章:
淺拷貝與深拷貝

3.3 insert

在指定位置增加一個元素

L= [1,2,3,3]
print(L.index(3))
# 2

3.4 del

del 是 Python 中的關鍵字,專門用來執行刪除操作,它不僅可以刪除整個列表,還可以刪除列表中的某些元素。
del 可以刪除列表中的單個元素,格式為:

del listname[index]

del 也可以刪除中間一段連續的元素,格式為:

del listname[start : end]

其中,start 表示起始索引,end 表示結束索引。del 會刪除從索引 start 到 end 之間的元素,不包括 end 位置的元素。

3.5 join

將列表變成字符串

#'str'.join(list)

li = ['my','name','is','bob'] 
print(' '.join(li))
# 'my name is bob' 
 
print('_'.join(li))
# 'my_name_is_bob' 

3.6 split

#split(seq,maxsplit=-1)

b = 'my..name..is..bob' 
print(b.split()) 
# ['my..name..is..bob'] 
 
print(b.split(".."))
# ['my', 'name', 'is', 'bob'] 

原創 https://www.cnblogs.com/gaoshaonian/p/16045599.html

3.7 reverse

此方法是對列表中的元素進行逆序

a = [1,3,6,8,9]
for i in reversed(a):
    print(i, end=" ")

除此之外還有其他兩種方法,舉例如下:

for i in a[::-1]:
    print(i, end=" ")

for i in range(len(a)-1,-1,-1):
    print(a[i], end=" ")

3.8 filter

filter() 函數用於過濾序列,過濾掉不符合條件的元素,返回由符合條件元素組成的新列表。
該接收兩個參數,第一個為函數,第二個為序列,序列的每個元素作為參數傳遞給函數進行判斷,然后返回 True 或 False,最后將返回 True 的元素放到新列表中。

def is_odd(n):
    return n % 2 == 1
 
newlist = filter(is_odd, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
print(newlist)
#[1, 3, 5, 7, 9]

本文參考了這個文章:
參考文章


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM